<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		
	<span class="different-posts different-posts_earlier">📖 <a href="/page/49">earlier posts</a> 📖</span>




	<article>
	
		<h1><a href="/2017/10/12/2. Linux 命令/67. Linux 命令- tee/">Linux 命令- tee</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Linux命令/">Linux命令</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;tee 命令用于读取标准输入的数据，并将其内容输出成文件。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;tee 指令会从标准输入设备读取数据，将其内容输出到标准输出设备，同时保存成文件。</p>
<h2 id="1-命令语法"><a href="#1-命令语法" class="headerlink" title="1. 命令语法"></a>1. 命令语法</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">tee [参数] [文件]</div></pre></td></tr></table></figure>
<h2 id="2-命令参数"><a href="#2-命令参数" class="headerlink" title="2. 命令参数"></a>2. 命令参数</h2><ul>
<li>-a或–append 　附加到既有文件的后面，而非覆盖它．</li>
<li>-i或–ignore-interrupts 　忽略中断信号。</li>
<li>–help 　在线帮助。</li>
<li>–version 　显示版本信息。</li>
</ul>
<h2 id="使用实例"><a href="#使用实例" class="headerlink" title="使用实例"></a>使用实例</h2><h3 id="实例1：使用指令-tee-将用户输入的数据同时保存到文件-file1-和-file2-中"><a href="#实例1：使用指令-tee-将用户输入的数据同时保存到文件-file1-和-file2-中" class="headerlink" title="实例1：使用指令 tee 将用户输入的数据同时保存到文件 file1 和 file2 中"></a>实例1：使用指令 tee 将用户输入的数据同时保存到文件 file1 和 file2 中</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">tee file1 file2</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># tee file1 file2</span></div><div class="line">my linux</div><div class="line">my linux</div><div class="line">^C</div><div class="line">[root@localhost ~]<span class="comment"># cat file1      </span></div><div class="line">my linux</div><div class="line">[root@localhost ~]<span class="comment"># cat file2      </span></div><div class="line">my linux</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;也可以使用管道符执行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># echo 'my linux'|tee file1 file2</span></div><div class="line">my linux</div><div class="line">[root@localhost ~]<span class="comment"># cat file1</span></div><div class="line">my linux</div><div class="line">[root@localhost ~]<span class="comment"># cat file2</span></div><div class="line">my linux</div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/2. Linux 命令/66. Linux 命令- uniq/">Linux 命令- uniq</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Linux命令/">Linux命令</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Linux uniq 命令用于检查及删除文本文件中重复的行列。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;uniq 可检查文本文件中重复出现的行列。</p>
<h2 id="1-命令语法"><a href="#1-命令语法" class="headerlink" title="1. 命令语法"></a>1. 命令语法</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">uniq [参数] [file]</div></pre></td></tr></table></figure>
<h2 id="2-命令参数"><a href="#2-命令参数" class="headerlink" title="2. 命令参数"></a>2. 命令参数</h2><ul>
<li>-c或–count 在每列旁边显示该行重复出现的次数。</li>
<li>-d或–repeated 仅显示重复出现的行列。</li>
<li>-f&lt;栏位&gt;或–skip-fields=&lt;栏位&gt; 忽略比较指定的栏位。</li>
<li>-s&lt;字符位置&gt;或–skip-chars=&lt;字符位置&gt; 忽略比较指定的字符。</li>
<li>-u或–unique 仅显示出一次的行列。</li>
<li>-w&lt;字符位置&gt;或–check-chars=&lt;字符位置&gt; 指定要比较的字符。</li>
<li>–help 显示帮助。</li>
<li>–version 显示版本信息。</li>
<li>[输入文件] 指定已排序好的文本文件。</li>
<li>[输出文件] 指定输出的文件。</li>
</ul>
<h2 id="3-使用实例"><a href="#3-使用实例" class="headerlink" title="3. 使用实例"></a>3. 使用实例</h2><h3 id="实例1：文件-testfile-中第2、5、9行为相同的行，使用-uniq-命令删除重复的行。"><a href="#实例1：文件-testfile-中第2、5、9行为相同的行，使用-uniq-命令删除重复的行。" class="headerlink" title="实例1：文件 testfile 中第2、5、9行为相同的行，使用 uniq 命令删除重复的行。"></a>实例1：文件 testfile 中第2、5、9行为相同的行，使用 uniq 命令删除重复的行。</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">uniq testfile</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">$ cat testfile      <span class="comment">#原有内容  </span></div><div class="line"><span class="built_in">test</span> 30  </div><div class="line"><span class="built_in">test</span> 30  </div><div class="line"><span class="built_in">test</span> 30  </div><div class="line">Hello 95  </div><div class="line">Hello 95  </div><div class="line">Hello 95  </div><div class="line">Hello 95  </div><div class="line">Linux 85  </div><div class="line">Linux 85 </div><div class="line">$ uniq testfile     <span class="comment">#删除重复行后的内容  </span></div><div class="line"><span class="built_in">test</span> 30  </div><div class="line">Hello 95  </div><div class="line">Linux 85</div></pre></td></tr></table></figure>
<h3 id="实例2：检查文件并删除文件中重复出现的行，并在行首显示该行重复出现的次数"><a href="#实例2：检查文件并删除文件中重复出现的行，并在行首显示该行重复出现的次数" class="headerlink" title="实例2：检查文件并删除文件中重复出现的行，并在行首显示该行重复出现的次数"></a>实例2：检查文件并删除文件中重复出现的行，并在行首显示该行重复出现的次数</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">uniq -c testfile</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">$ uniq-ctestfile      <span class="comment">#删除重复行后的内容  </span></div><div class="line">3 <span class="built_in">test</span> 30             <span class="comment">#前面的数字的意义为该行共出现了3次  </span></div><div class="line">4 Hello 95            <span class="comment">#前面的数字的意义为该行共出现了4次  </span></div><div class="line">2 Linux 85            <span class="comment">#前面的数字的意义为该行共出现了2次</span></div></pre></td></tr></table></figure>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/2. Linux 命令/65. Linux 命令- sort/">Linux 命令- sort</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Linux命令/">Linux命令</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;sort 命令在 linux 里非常有用，它将文件进行排序，并将排序结果标准输出。</p>
<h2 id="1-命令语法"><a href="#1-命令语法" class="headerlink" title="1. 命令语法"></a>1. 命令语法</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">sort [参数] [文件]</div></pre></td></tr></table></figure>
<h2 id="2-命令功能"><a href="#2-命令功能" class="headerlink" title="2. 命令功能"></a>2. 命令功能</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;用作排序</p>
<h2 id="3-命令参数"><a href="#3-命令参数" class="headerlink" title="3. 命令参数"></a>3. 命令参数</h2><ul>
<li>-b   忽略每行前面开始出的空格字符。</li>
<li>-d   排序时，处理英文字母、数字及空格字符外，忽略其他的字符。</li>
<li>-f   排序时，将小写字母视为大写字母。</li>
<li>-i   排序时，除了040至176之间的ASCII字符外，忽略其他的字符。</li>
<li>-c   检查文件是否已经按照顺序排序。</li>
<li>-m   将几个排序好的文件进行合并。</li>
<li>-M  前面3个字母依照月份的缩写进行排序。</li>
<li>-n   依照数值的大小排序。</li>
<li>-o&lt;输出文件&gt;   将排序后的结果存入指定的文件。</li>
<li>-r   以相反的顺序来排序。</li>
<li>-t&lt;分隔字符&gt;   指定排序时所用的栏位分隔字符。</li>
<li>-k 指定域</li>
<li>–help   显示帮助。</li>
<li>–version   显示版本信息。</li>
</ul>
<h2 id="使用实例"><a href="#使用实例" class="headerlink" title="使用实例"></a>使用实例</h2><h3 id="实例1：对-etc-passwd-的帐号进行排序"><a href="#实例1：对-etc-passwd-的帐号进行排序" class="headerlink" title="实例1：对 /etc/passwd 的帐号进行排序"></a>实例1：对 /etc/passwd 的帐号进行排序</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">cat /etc/passwd|sort</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># cat /etc/passwd|sort</span></div><div class="line">adm:x:3:4:adm:/var/adm:/sbin/nologin</div><div class="line">bin:x:1:1:bin:/bin:/sbin/nologin</div><div class="line">chrony:x:997:995::/var/lib/chrony:/sbin/nologin</div><div class="line">daemon:x:2:2:daemon:/sbin:/sbin/nologin</div><div class="line">dbus:x:81:81:System message bus:/:/sbin/nologin</div><div class="line">ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin</div><div class="line">games:x:12:100:games:/usr/games:/sbin/nologin</div><div class="line">halt:x:7:0:halt:/sbin:/sbin/halt</div><div class="line">lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin</div><div class="line">mail:x:8:12:mail:/var/spool/mail:/sbin/nologin</div><div class="line">nobody:x:99:99:Nobody:/:/sbin/nologin</div><div class="line">operator:x:11:0:operator:/root:/sbin/nologin</div><div class="line">polkitd:x:998:996:User <span class="keyword">for</span> polkitd:/:/sbin/nologin</div><div class="line">postfix:x:89:89::/var/spool/postfix:/sbin/nologin</div><div class="line">root:x:0:0:root:/root:/bin/bash</div><div class="line">shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown</div><div class="line">sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin</div><div class="line">sync:x:5:0:sync:/sbin:/bin/sync</div><div class="line">systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin</div><div class="line">systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin</div><div class="line">tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果 sort 不加任何选项，则从手自缚向后，一次按 ASCII 码值进行比较，最后将它们按升序输出。</p>
<h3 id="实例2：对-etc-passwd-第三栏排序"><a href="#实例2：对-etc-passwd-第三栏排序" class="headerlink" title="实例2：对 /etc/passwd 第三栏排序"></a>实例2：对 /etc/passwd 第三栏排序</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">cat /etc/passwd|sort -t<span class="string">':'</span> -k 3</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># cat /etc/passwd|sort -t':' -k 3</span></div><div class="line">root:x:0:0:root:/root:/bin/bash</div><div class="line">operator:x:11:0:operator:/root:/sbin/nologin</div><div class="line">bin:x:1:1:bin:/bin:/sbin/nologin</div><div class="line">games:x:12:100:games:/usr/games:/sbin/nologin</div><div class="line">ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin</div><div class="line">systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin</div><div class="line">daemon:x:2:2:daemon:/sbin:/sbin/nologin</div><div class="line">adm:x:3:4:adm:/var/adm:/sbin/nologin</div><div class="line">lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin</div><div class="line">sync:x:5:0:sync:/sbin:/bin/sync</div><div class="line">tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin</div><div class="line">shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown</div><div class="line">halt:x:7:0:halt:/sbin:/sbin/halt</div><div class="line">sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin</div><div class="line">mail:x:8:12:mail:/var/spool/mail:/sbin/nologin</div><div class="line">dbus:x:81:81:System message bus:/:/sbin/nologin</div><div class="line">postfix:x:89:89::/var/spool/postfix:/sbin/nologin</div><div class="line">chrony:x:997:995::/var/lib/chrony:/sbin/nologin</div><div class="line">polkitd:x:998:996:User <span class="keyword">for</span> polkitd:/:/sbin/nologin</div><div class="line">systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin</div><div class="line">nobody:x:99:99:Nobody:/:/sbin/nologin</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;默认是以字符串来排序的</p>
<h3 id="实例3：对-etc-passwd-第三栏以纯数字排序"><a href="#实例3：对-etc-passwd-第三栏以纯数字排序" class="headerlink" title="实例3：对 /etc/passwd 第三栏以纯数字排序"></a>实例3：对 /etc/passwd 第三栏以纯数字排序</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">cat /etc/passwd|sort -t<span class="string">':'</span> -k3 -n</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># cat /etc/passwd|sort -t':' -k 3 -n</span></div><div class="line">root:x:0:0:root:/root:/bin/bash</div><div class="line">bin:x:1:1:bin:/bin:/sbin/nologin</div><div class="line">daemon:x:2:2:daemon:/sbin:/sbin/nologin</div><div class="line">adm:x:3:4:adm:/var/adm:/sbin/nologin</div><div class="line">lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin</div><div class="line">sync:x:5:0:sync:/sbin:/bin/sync</div><div class="line">shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown</div><div class="line">halt:x:7:0:halt:/sbin:/sbin/halt</div><div class="line">mail:x:8:12:mail:/var/spool/mail:/sbin/nologin</div><div class="line">operator:x:11:0:operator:/root:/sbin/nologin</div><div class="line">games:x:12:100:games:/usr/games:/sbin/nologin</div><div class="line">ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin</div><div class="line">tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin</div><div class="line">sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin</div><div class="line">dbus:x:81:81:System message bus:/:/sbin/nologin</div><div class="line">postfix:x:89:89::/var/spool/postfix:/sbin/nologin</div><div class="line">nobody:x:99:99:Nobody:/:/sbin/nologin</div><div class="line">systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin</div><div class="line">chrony:x:997:995::/var/lib/chrony:/sbin/nologin</div><div class="line">polkitd:x:998:996:User <span class="keyword">for</span> polkitd:/:/sbin/nologin</div><div class="line">systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin</div></pre></td></tr></table></figure>
<h3 id="实例4：去重复排序"><a href="#实例4：去重复排序" class="headerlink" title="实例4：去重复排序"></a>实例4：去重复排序</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">sort -u seq.txt</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line">[rocrocket@rocrocket programming]$ cat seq.txt</div><div class="line">banana</div><div class="line">apple</div><div class="line">pear</div><div class="line">orange</div><div class="line">pear</div><div class="line">[rocrocket@rocrocket programming]$ sort seq.txt</div><div class="line">apple</div><div class="line">banana</div><div class="line">orange</div><div class="line">pear</div><div class="line">pear</div><div class="line">[rocrocket@rocrocket programming]$ sort -u seq.txt</div><div class="line">apple</div><div class="line">banana</div><div class="line">orange</div><div class="line">pear</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;pear 由于重复被 -u 选项去重复了</p>
<h3 id="实例5：进行降序排列"><a href="#实例5：进行降序排列" class="headerlink" title="实例5：进行降序排列"></a>实例5：进行降序排列</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">sort -r number.txt</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line">rocrocket@rocrocket programming]$ cat number.txt</div><div class="line">1</div><div class="line">3</div><div class="line">5</div><div class="line">2</div><div class="line">4</div><div class="line">[rocrocket@rocrocket programming]$ sort number.txt</div><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">[rocrocket@rocrocket programming]$ sort -r number.txt</div><div class="line">5</div><div class="line">4</div><div class="line">3</div><div class="line">2</div><div class="line">1</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;sort 默认的排列方式是升序，使用 -r 选项就可以改成降序</p>
<h3 id="实例6：把排序结果输出到源文件中"><a href="#实例6：把排序结果输出到源文件中" class="headerlink" title="实例6：把排序结果输出到源文件中"></a>实例6：把排序结果输出到源文件中</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">sort -r number.txt -o number.txt</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line">[rocrocket@rocrocket programming]$ sort -r number.txt &gt; number.txt</div><div class="line">[rocrocket@rocrocket programming]$ cat number.txt</div><div class="line">[rocrocket@rocrocket programming]$</div><div class="line"></div><div class="line">[rocrocket@rocrocket programming]$ cat number.txt</div><div class="line">1</div><div class="line">3</div><div class="line">5</div><div class="line">2</div><div class="line">4</div><div class="line">[rocrocket@rocrocket programming]$ sort -r number.txt -o number.txt</div><div class="line">[rocrocket@rocrocket programming]$ cat number.txt</div><div class="line">5</div><div class="line">4</div><div class="line">3</div><div class="line">2</div><div class="line">1</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;直接把排序结果用重定向输出到源文件中，结果文件被清空了；使用 -o 选项解决了这个问题，可以放心把输出结果写入源文件</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/2. Linux 命令/64. Linux 命令- cut/">Linux 命令- cut</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Linux命令/">Linux命令</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;cut命令用来显示行中的指定部分，删除文件中指定字段。cut经常用来显示文件的内容，类似于下的type命令。</p>
<h2 id="1-命令格式"><a href="#1-命令格式" class="headerlink" title="1. 命令格式"></a>1. 命令格式</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">cut [参数] [file]</div></pre></td></tr></table></figure>
<h2 id="2-命令功能"><a href="#2-命令功能" class="headerlink" title="2. 命令功能"></a>2. 命令功能</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;cut 命令从文件的每一行剪切字节、字符和字段并将这些字节、字符和字段写至标准输出。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果不指定 File 参数，cut 命令将读取标准输入。必须指定 -b、-c 或 -f 标志之一。</p>
<h2 id="3-命令参数"><a href="#3-命令参数" class="headerlink" title="3. 命令参数"></a>3. 命令参数</h2><ul>
<li>-b：仅显示行中指定直接范围的内容；</li>
<li>-c：仅显示行中指定范围的字符；</li>
<li>-d：指定字段的分隔符，默认的字段分隔符为“TAB”；</li>
<li>-f：显示指定字段的内容；</li>
<li>-n：与“-b”选项连用，不分割多字节字符；</li>
<li>–complement：补足被选择的字节、字符或字段； </li>
<li>–out-delimiter=&lt;字段分隔符&gt;：指定输出内容是的字段分割符；</li>
<li>–help：显示指令的帮助信息</li>
<li>–version：显示指令的版本信息。</li>
</ul>
<h2 id="4-使用实例"><a href="#4-使用实例" class="headerlink" title="4. 使用实例"></a>4. 使用实例</h2><h3 id="实例1：截取文件其中一个字符"><a href="#实例1：截取文件其中一个字符" class="headerlink" title="实例1：截取文件其中一个字符"></a>实例1：截取文件其中一个字符</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">cut -c2 /etc/passwd</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># cut -c2 /etc/passwd</span></div><div class="line">o</div><div class="line">i</div><div class="line">a</div><div class="line">d</div><div class="line">p</div><div class="line">y</div><div class="line">h</div><div class="line">a</div><div class="line">a</div><div class="line">p</div><div class="line">a</div><div class="line">t</div><div class="line">o</div><div class="line">y</div><div class="line">y</div><div class="line">b</div><div class="line">o</div><div class="line">s</div><div class="line">o</div><div class="line">s</div><div class="line">h</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;提取第2个字符</p>
<h3 id="实例2：截取文件多个字符"><a href="#实例2：截取文件多个字符" class="headerlink" title="实例2：截取文件多个字符"></a>实例2：截取文件多个字符</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">cut -c2-5,10 /etc/passwd</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># cut -c2-5,10 /etc/passwd</span></div><div class="line">oot:0</div><div class="line"><span class="keyword">in</span>:x:</div><div class="line">aemo2</div><div class="line">dm:x:</div><div class="line">p:x:l</div><div class="line">ync:0</div><div class="line">hutdx</div><div class="line">alt:0</div><div class="line">ail:1</div><div class="line">perax</div><div class="line">ames2</div><div class="line">tp:x5</div><div class="line">obod9</div><div class="line">ysteu</div><div class="line">ystee</div><div class="line">bus::</div><div class="line">olki:</div><div class="line">ss:x5</div><div class="line">ostf:</div><div class="line">shd::</div><div class="line">hron9</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;提取第2、第3、第4、第5和第10个字符</p>
<h3 id="实例3：提取前4个字符"><a href="#实例3：提取前4个字符" class="headerlink" title="实例3：提取前4个字符"></a>实例3：提取前4个字符</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">cut -c-4 /etc/passwd</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># cut -c-4 /etc/passwd</span></div><div class="line">root</div><div class="line">bin:</div><div class="line">daem</div><div class="line">adm:</div><div class="line">lp:x</div><div class="line">sync</div><div class="line">shut</div><div class="line">halt</div><div class="line">mail</div><div class="line">oper</div><div class="line">game</div><div class="line">ftp:</div><div class="line">nobo</div><div class="line">syst</div><div class="line">syst</div><div class="line">dbus</div><div class="line">polk</div><div class="line">tss:</div><div class="line">post</div><div class="line">sshd</div><div class="line">chro</div></pre></td></tr></table></figure>
<h3 id="实例4：打印从第10个字符开始到结尾"><a href="#实例4：打印从第10个字符开始到结尾" class="headerlink" title="实例4：打印从第10个字符开始到结尾"></a>实例4：打印从第10个字符开始到结尾</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">cut -c10- /etc/passwd</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># cut -c5- /etc/passwd</span></div><div class="line">:x:0:0:root:/root:/bin/bash</div><div class="line">x:1:1:bin:/bin:/sbin/nologin</div><div class="line">on:x:2:2:daemon:/sbin:/sbin/nologin</div><div class="line">x:3:4:adm:/var/adm:/sbin/nologin</div><div class="line">:4:7:lp:/var/spool/lpd:/sbin/nologin</div><div class="line">:x:5:0:sync:/sbin:/bin/sync</div><div class="line">down:x:6:0:shutdown:/sbin:/sbin/shutdown</div><div class="line">:x:7:0:halt:/sbin:/sbin/halt</div><div class="line">:x:8:12:mail:/var/spool/mail:/sbin/nologin</div><div class="line">ator:x:11:0:operator:/root:/sbin/nologin</div><div class="line">s:x:12:100:games:/usr/games:/sbin/nologin</div><div class="line">x:14:50:FTP User:/var/ftp:/sbin/nologin</div><div class="line">dy:x:99:99:Nobody:/:/sbin/nologin</div><div class="line">emd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin</div><div class="line">emd-network:x:192:192:systemd Network Management:/:/sbin/nologin</div><div class="line">:x:81:81:System message bus:/:/sbin/nologin</div><div class="line">itd:x:998:996:User <span class="keyword">for</span> polkitd:/:/sbin/nologin</div><div class="line">x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin</div><div class="line">fix:x:89:89::/var/spool/postfix:/sbin/nologin</div><div class="line">:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin</div><div class="line">ny:x:997:995::/var/lib/chrony:/sbin/nologin</div></pre></td></tr></table></figure>
<h3 id="实例5：用-f-提取指定字段，-d-指定分隔符"><a href="#实例5：用-f-提取指定字段，-d-指定分隔符" class="headerlink" title="实例5：用 -f 提取指定字段，-d 指定分隔符"></a>实例5：用 -f 提取指定字段，-d 指定分隔符</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">cut -f1 -d<span class="string">" "</span> test.txt</div><div class="line">cut -f2,3 -d<span class="string">" "</span> test.txt</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># cat test.txt</span></div><div class="line">No Name Mark Percent</div><div class="line">01 tom 69 91</div><div class="line">02 jack 71 87</div><div class="line">03 alex 68 98</div><div class="line">[root@localhost ~]<span class="comment"># cut -f1 -d" " test.txt</span></div><div class="line">No</div><div class="line">01</div><div class="line">02</div><div class="line">03</div><div class="line">[root@localhost ~]<span class="comment"># cut -f2,3 -d" " test.txt</span></div><div class="line">Name Mark</div><div class="line">tom 69</div><div class="line">jack 71</div><div class="line">alex 68</div></pre></td></tr></table></figure>
<h3 id="实例6：使用-–complement-提取指定字段之外的列"><a href="#实例6：使用-–complement-提取指定字段之外的列" class="headerlink" title="实例6：使用 –complement 提取指定字段之外的列"></a>实例6：使用 –complement 提取指定字段之外的列</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">cut -f2 -d<span class="string">" "</span> --complement test.txt</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># cat test.txt                 </span></div><div class="line">No Name Mark Percent</div><div class="line">01 tom 69 91</div><div class="line">02 jack 71 87</div><div class="line">03 alex 68 98</div><div class="line">[root@localhost ~]<span class="comment"># cut -f2 -d" " --complement test.txt</span></div><div class="line">No Mark Percent</div><div class="line">01 69 91</div><div class="line">02 71 87</div><div class="line">03 68 98</div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/2. Linux 命令/63. Linux 命令- wget/">Linux 命令- wget</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Linux命令/">Linux命令</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Linux系统中的wget是一个下载文件的工具，它用在命令行下。对于Linux用户是必不可少的工具，我们经常要下载一些软件或从远程服务器恢复备份到本地服务器。wget支持HTTP，HTTPS和FTP协议，可以使用HTTP代理。所谓的自动下载是指，wget可以在用户退出系统的之后在后台执行。这意味这你可以登录系统，启动一个wget下载任务，然后退出系统，wget将在后台执行直到任务完成，相对于其它大部分浏览器在下载大量数据时需要用户一直的参与，这省去了极大的麻烦。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;wget 可以跟踪HTML页面上的链接依次下载来创建远程服务器的本地版本，完全重建原始站点的目录结构。这又常被称作”递归下载”。在递归下载的时候，wget 遵循Robot Exclusion标准(/robots.txt). wget可以在下载的同时，将链接转换成指向本地文件，以方便离线浏览。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;wget         非常稳定，它在带宽很窄的情况下和不稳定网络中有很强的适应性.如果是由于网络的原因下载失败，wget会不断的尝试，直到整个文件下载完毕。如果是服务器打断下载过程，它会再次联到服务器上从停止的地方继续下载。这对从那些限定了链接时间的服务器上下载大文件非常有用。</p>
<h2 id="1．命令格式"><a href="#1．命令格式" class="headerlink" title="1．命令格式"></a>1．命令格式</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">wget [参数] [URL地址]</div></pre></td></tr></table></figure>
<h2 id="2．命令功能"><a href="#2．命令功能" class="headerlink" title="2．命令功能"></a>2．命令功能</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;用于从网络上下载资源，没有指定目录，下载资源回默认为当前目录。wget虽然功能强大，但是使用起来还是比较简单：</p>
<ol>
<li>支持断点下传功能；这一点，也是网络蚂蚁和FlashGet当年最大的卖点，现在，Wget也可以使用此功能，那些网络不是太好的用户可以放心了；</li>
<li>同时支持FTP和HTTP下载方式；尽管现在大部分软件可以使用HTTP方式下载，但是，有些时候，仍然需要使用FTP方式下载软件；</li>
<li>支持代理服务器；对安全强度很高的系统而言，一般不会将自己的系统直接暴露在互联网上，所以，支持代理是下载软件必须有的功能；</li>
<li>设置方便简单；可能，习惯图形界面的用户已经不是太习惯命令行了，但是，命令行在设置上其实有更多的优点，最少，鼠标可以少点很多次，也不要担心是否错点鼠标；</li>
<li>程序小，完全免费；程序小可以考虑不计，因为现在的硬盘实在太大了；完全免费就不得不考虑了，即使网络上有很多所谓的免费软件，但是，这些软件的广告却不是我们喜欢的。</li>
</ol>
<h2 id="3．命令参数："><a href="#3．命令参数：" class="headerlink" title="3．命令参数："></a>3．命令参数：</h2><h3 id="启动参数："><a href="#启动参数：" class="headerlink" title="启动参数："></a>启动参数：</h3><ul>
<li>-V, –version 显示wget的版本后退出</li>
<li>-h, –help 打印语法帮助</li>
<li>-b, –background 启动后转入后台执行</li>
<li>-e, –execute=COMMAND 执行`.wgetrc’格式的命令，wgetrc格式参见/etc/wgetrc或~/.wgetrc</li>
</ul>
<h3 id="记录和输入文件参数："><a href="#记录和输入文件参数：" class="headerlink" title="记录和输入文件参数："></a>记录和输入文件参数：</h3><ul>
<li>-o, –output-file=FILE 把记录写到FILE文件中</li>
<li>-a, –append-output=FILE 把记录追加到FILE文件中</li>
<li>-d, –debug 打印调试输出</li>
<li>-q, –quiet 安静模式(没有输出)</li>
<li>-v, –verbose 冗长模式(这是缺省设置)</li>
<li>-nv, –non-verbose 关掉冗长模式，但不是安静模式</li>
<li>-i, –input-file=FILE 下载在FILE文件中出现的URLs</li>
<li>-F, –force-html 把输入文件当作HTML格式文件对待</li>
<li>-B, –base=URL 将URL作为在-F - -i参数指定的文件中出现的相对链接的前缀</li>
<li>–sslcertfile=FILE 可选客户端证书</li>
<li>–sslcertkey=KEYFILE 可选客户端证书的KEYFILE</li>
<li>–egd-file=FILE 指定EGD socket的文件名</li>
</ul>
<h3 id="下载参数："><a href="#下载参数：" class="headerlink" title="下载参数："></a>下载参数：</h3><ul>
<li>–bind-address=ADDRESS 指定本地使用地址(主机名或IP，当本地有多个IP或名字时使用)</li>
<li>-t, –tries=NUMBER 设定最大尝试链接次数(0 表示无限制).</li>
<li>-O –output-document=FILE 把文档写到FILE文件中</li>
<li>-nc, –no-clobber 不要覆盖存在的文件或使用.#前缀</li>
<li>-c, –continue 接着下载没下载完的文件</li>
<li>–progress=TYPE 设定进程条标记</li>
<li>-N, –timestamping 不要重新下载文件除非比本地文件新</li>
<li>-S, –server-response 打印服务器的回应</li>
<li>–spider 不下载任何东西</li>
<li>-T, –timeout=SECONDS 设定响应超时的秒数</li>
<li>-w, –wait=SECONDS 两次尝试之间间隔SECONDS秒</li>
<li>–waitretry=SECONDS 在重新链接之间等待1…SECONDS秒</li>
<li>–random-wait 在下载之间等待0…2*WAIT秒</li>
<li>-Y, –proxy=on/off 打开或关闭代理</li>
<li>-Q, –quota=NUMBER 设置下载的容量限制</li>
<li>–limit-rate=RATE 限定下载输率</li>
</ul>
<h3 id="目录参数："><a href="#目录参数：" class="headerlink" title="目录参数："></a>目录参数：</h3><ul>
<li>-nd –no-directories 不创建目录</li>
<li>-x, –force-directories 强制创建目录</li>
<li>-nH, –no-host-directories 不创建主机目录</li>
<li>-P, –directory-prefix=PREFIX 将文件保存到目录 PREFIX/…</li>
<li>–cut-dirs=NUMBER 忽略 NUMBER层远程目录</li>
</ul>
<h3 id="HTTP-选项参数："><a href="#HTTP-选项参数：" class="headerlink" title="HTTP 选项参数："></a>HTTP 选项参数：</h3><ul>
<li>–http-user=USER 设定HTTP用户名为 USER.</li>
<li>–http-passwd=PASS 设定http密码为 PASS</li>
<li>-C, –cache=on/off 允许/不允许服务器端的数据缓存 (一般情况下允许)</li>
<li>-E, –html-extension 将所有text/html文档以.html扩展名保存</li>
<li>–ignore-length 忽略 `Content-Length’头域</li>
<li>–header=STRING 在headers中插入字符串 STRING</li>
<li>–proxy-user=USER 设定代理的用户名为 USER</li>
<li>–proxy-passwd=PASS 设定代理的密码为 PASS</li>
<li>–referer=URL 在HTTP请求中包含 `Referer: URL’头</li>
<li>-s, –save-headers 保存HTTP头到文件</li>
<li>-U, –user-agent=AGENT 设定代理的名称为 AGENT而不是 Wget/VERSION</li>
<li>–no-http-keep-alive 关闭 HTTP活动链接 (永远链接)</li>
<li>–cookies=off 不使用 cookies</li>
<li>–load-cookies=FILE 在开始会话前从文件 FILE中加载cookie</li>
<li>–save-cookies=FILE 在会话结束后将 cookies保存到 FILE文件中</li>
</ul>
<h3 id="FTP-选项参数："><a href="#FTP-选项参数：" class="headerlink" title="FTP 选项参数："></a>FTP 选项参数：</h3><ul>
<li>-nr, –dont-remove-listing 不移走 `.listing’文件</li>
<li>-g, –glob=on/off 打开或关闭文件名的 globbing机制</li>
<li>–passive-ftp 使用被动传输模式 (缺省值).</li>
<li>–active-ftp 使用主动传输模式</li>
<li>–retr-symlinks 在递归的时候，将链接指向文件(而不是目录)</li>
</ul>
<h3 id="递归下载参数："><a href="#递归下载参数：" class="headerlink" title="递归下载参数："></a>递归下载参数：</h3><ul>
<li>-r, –recursive 递归下载－－慎用!</li>
<li>-l, –level=NUMBER 最大递归深度 (inf 或 0 代表无穷)</li>
<li>–delete-after 在现在完毕后局部删除文件</li>
<li>-k, –convert-links 转换非相对链接为相对链接</li>
<li>-K, –backup-converted 在转换文件X之前，将之备份为 X.orig</li>
<li>-m, –mirror 等价于 -r -N -l inf -nr</li>
<li>-p, –page-requisites 下载显示HTML文件的所有图片</li>
</ul>
<h3 id="递归下载中的包含和不包含-accept-reject-："><a href="#递归下载中的包含和不包含-accept-reject-：" class="headerlink" title="递归下载中的包含和不包含(accept/reject)："></a>递归下载中的包含和不包含(accept/reject)：</h3><ul>
<li>-A, –accept=LIST 分号分隔的被接受扩展名的列表</li>
<li>-R, –reject=LIST 分号分隔的不被接受的扩展名的列表</li>
<li>-D, –domains=LIST 分号分隔的被接受域的列表</li>
<li>–exclude-domains=LIST 分号分隔的不被接受的域的列表</li>
<li>–follow-ftp 跟踪HTML文档中的FTP链接</li>
<li>–follow-tags=LIST 分号分隔的被跟踪的HTML标签的列表</li>
<li>-G, –ignore-tags=LIST 分号分隔的被忽略的HTML标签的列表</li>
<li>-H, –span-hosts 当递归时转到外部主机</li>
<li>-L, –relative 仅仅跟踪相对链接</li>
<li>-I, –include-directories=LIST 允许目录的列表</li>
<li>-X, –exclude-directories=LIST 不被包含目录的列表</li>
<li>-np, –no-parent 不要追溯到父目录<br>wget -S –spider url 不下载只显示过程</li>
</ul>
<h2 id="4．使用实例"><a href="#4．使用实例" class="headerlink" title="4．使用实例"></a>4．使用实例</h2><h3 id="实例1：使用wget下载单个文件"><a href="#实例1：使用wget下载单个文件" class="headerlink" title="实例1：使用wget下载单个文件"></a>实例1：使用wget下载单个文件</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">wget http://www.minjieren.com/wordpress-3.1-zh_CN.zip</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以下的例子是从网络下载一个文件并保存在当前目录，在下载的过程中会显示进度条，包含（下载完成百分比，已经下载的字节，当前下载速度，剩余下载时间）。</p>
<h3 id="实例2：使用wget-O下载并以不同的文件名保存"><a href="#实例2：使用wget-O下载并以不同的文件名保存" class="headerlink" title="实例2：使用wget -O下载并以不同的文件名保存"></a>实例2：使用wget -O下载并以不同的文件名保存</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">wget -O wordpress.zip http://www.minjieren.com/download.aspx?id=1080</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;wget默认会以最后一个符合”/”的后面的字符来命令，对于动态链接的下载通常文件名会不正确。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;错误：下面的例子会下载一个文件并以名称download.aspx?id=1080保存</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">wget http://www.minjieren.com/download?id=1</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;即使下载的文件是zip格式，它仍然以download.php?id=1080命令。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;正确：为了解决这个问题，我们可以使用参数-O来指定一个文件名：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">wget -O wordpress.zip http://www.minjieren.com/download.aspx?id=1080</div></pre></td></tr></table></figure>
<h3 id="实例3：使用wget-–limit-rate限速下载"><a href="#实例3：使用wget-–limit-rate限速下载" class="headerlink" title="实例3：使用wget –limit -rate限速下载"></a>实例3：使用wget –limit -rate限速下载</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">wget --<span class="built_in">limit</span>-rate=300k http://www.minjieren.com/wordpress-3.1-zh_CN.zip</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当你执行wget的时候，它默认会占用全部可能的宽带下载。但是当你准备下载一个大文件，而你还需要下载其它文件时就有必要限速了。</p>
<h3 id="实例4：使用wget-c断点续传"><a href="#实例4：使用wget-c断点续传" class="headerlink" title="实例4：使用wget -c断点续传"></a>实例4：使用wget -c断点续传</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">wget -c http://www.minjieren.com/wordpress-3.1-zh_CN.zip</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;使用wget -c重新启动下载中断的文件，对于我们下载大文件时突然由于网络等原因中断非常有帮助，我们可以继续接着下载而不是重新下载一个文件。需要继续中断的下载时可以使用-c参数。</p>
<h3 id="实例5：使用wget-b后台下载"><a href="#实例5：使用wget-b后台下载" class="headerlink" title="实例5：使用wget -b后台下载"></a>实例5：使用wget -b后台下载</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">wget -b http://www.minjieren.com/wordpress-3.1-zh_CN.zip</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;对于下载非常大的文件的时候，我们可以使用参数-b进行后台下载。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">wget -b http://www.minjieren.com/wordpress-3.1-zh_CN.zip</div><div class="line">Continuing <span class="keyword">in</span> background, pid 1840.</div><div class="line">Output will be written to `wget-log<span class="string">'.</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;你可以使用以下命令来察看下载进度：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">tail -f wget-log</div></pre></td></tr></table></figure>
<h3 id="实例6：伪装代理名称下载"><a href="#实例6：伪装代理名称下载" class="headerlink" title="实例6：伪装代理名称下载"></a>实例6：伪装代理名称下载</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">wget --user-agent=<span class="string">"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.204 Safari/534.16"</span> http://www.minjieren.com/wordpress-3.1-zh_CN.zip</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;有些网站能通过根据判断代理名称不是浏览器而拒绝你的下载请求。不过你可以通过–user-agent参数伪装。</p>
<h3 id="实例7：使用wget-–spider测试下载链接"><a href="#实例7：使用wget-–spider测试下载链接" class="headerlink" title="实例7：使用wget –spider测试下载链接"></a>实例7：使用wget –spider测试下载链接</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">wget --spider URL</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当你打算进行定时下载，你应该在预定时间测试下载链接是否有效。我们可以增加–spider参数进行检查。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">wget --spider URL</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果下载链接正确，将会显示</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">wget --spider URL</div><div class="line">Spider mode enabled. Check <span class="keyword">if</span> remote file exists.</div><div class="line">HTTP request sent, awaiting response... 200 OK</div><div class="line">Length: unspecified [text/html]</div><div class="line">Remote file exists and could contain further links,</div><div class="line">but recursion is disabled -- not retrieving.</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这保证了下载能在预定的时间进行，但当你给错了一个链接，将会显示如下错误</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">wget --spider url</div><div class="line">Spider mode enabled. Check <span class="keyword">if</span> remote file exists.</div><div class="line">HTTP request sent, awaiting response... 404 Not Found</div><div class="line">Remote file does not exist -- broken link!!!</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;可以在以下几种情况下使用spider参数：</p>
<ul>
<li>定时下载之前进行检查</li>
<li>间隔检测网站是否可用</li>
<li>检查网站页面的死链接</li>
</ul>
<h3 id="实例8：使用wget-–tries增加重试次数"><a href="#实例8：使用wget-–tries增加重试次数" class="headerlink" title="实例8：使用wget –tries增加重试次数"></a>实例8：使用wget –tries增加重试次数</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">wget --tries=40 URL</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果网络有问题或下载一个大文件也有可能失败。wget默认重试20次连接下载文件。如果需要，你可以使用–tries增加重试次数。</p>
<h3 id="实例9：使用wget-i下载多个文件"><a href="#实例9：使用wget-i下载多个文件" class="headerlink" title="实例9：使用wget -i下载多个文件"></a>实例9：使用wget -i下载多个文件</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">wget -i filelist.txt</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先，保存一份下载链接文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">cat &gt; filelist.txt</div><div class="line">url1</div><div class="line">url2</div><div class="line">url3</div><div class="line">url4</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;接着使用这个文件和参数-i下载</p>
<h3 id="实例10：使用wget-–mirror镜像网站"><a href="#实例10：使用wget-–mirror镜像网站" class="headerlink" title="实例10：使用wget –mirror镜像网站"></a>实例10：使用wget –mirror镜像网站</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">wget --mirror -p --convert-links -P ./LOCAL URL</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下载整个网站到本地。</p>
<ul>
<li>–miror:开户镜像下载</li>
<li>-p:下载所有为了html页面显示正常的文件</li>
<li>–convert-links:下载后，转换成本地的链接</li>
<li>-P ./LOCAL：保存所有文件和目录到本地指定目录</li>
</ul>
<h3 id="实例11：使用wget-–reject过滤指定格式下载"><a href="#实例11：使用wget-–reject过滤指定格式下载" class="headerlink" title="实例11：使用wget –reject过滤指定格式下载"></a>实例11：使用wget –reject过滤指定格式下载</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">wget --reject=gif ur</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下载一个网站，但你不希望下载图片，可以使用以下命令。</p>
<h3 id="实例12：使用wget-o把下载信息存入日志文件"><a href="#实例12：使用wget-o把下载信息存入日志文件" class="headerlink" title="实例12：使用wget -o把下载信息存入日志文件"></a>实例12：使用wget -o把下载信息存入日志文件</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">wget -o download.log URL</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;不希望下载信息直接显示在终端而是在一个日志文件，可以使用</p>
<h3 id="实例13：使用wget-Q限制总下载文件大小"><a href="#实例13：使用wget-Q限制总下载文件大小" class="headerlink" title="实例13：使用wget -Q限制总下载文件大小"></a>实例13：使用wget -Q限制总下载文件大小</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">wget -Q5m -i filelist.txt</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当你想要下载的文件超过5M而退出下载，你可以使用。注意：这个参数对单个文件下载不起作用，只能递归下载时才有效。</p>
<h3 id="实例14：使用wget-r-A下载指定格式文件"><a href="#实例14：使用wget-r-A下载指定格式文件" class="headerlink" title="实例14：使用wget -r -A下载指定格式文件"></a>实例14：使用wget -r -A下载指定格式文件</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">wget -r -A.pdf url</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;可以在以下情况使用该功能：</p>
<ul>
<li>下载一个网站的所有图片</li>
<li>下载一个网站的所有视频</li>
<li>下载一个网站的所有PDF文件</li>
</ul>
<h3 id="实例15：使用wget-FTP下载"><a href="#实例15：使用wget-FTP下载" class="headerlink" title="实例15：使用wget FTP下载"></a>实例15：使用wget FTP下载</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">wget ftp-url</div><div class="line">wget --ftp-user=USERNAME --ftp-password=PASSWORD url</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;可以使用wget来完成ftp链接的下载。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;使用wget匿名ftp下载：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">wget ftp-url</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;使用wget用户名和密码认证的ftp下载</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">wget --ftp-user=USERNAME --ftp-password=PASSWORD url</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;备注：编译安装<br>使用如下命令编译安装： </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># tar zxvf wget-1.9.1.tar.gz </span></div><div class="line"><span class="comment"># cd wget-1.9.1 </span></div><div class="line"><span class="comment"># ./configure </span></div><div class="line"><span class="comment"># make </span></div><div class="line"><span class="comment"># make install</span></div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/2. Linux 命令/62. Linux 命令- scp/">Linux 命令- scp</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Linux命令/">Linux命令</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;scp是secure copy的简写，用于在Linux下进行远程拷贝文件的命令，和它类似的命令有cp，不过cp只是在本机进行拷贝不能跨服务器，而且scp传输是加密的。可能会稍微影响一下速度。当你服务器硬盘变为只读 read only system时，用scp可以帮你把文件移出来。另外，scp还非常不占资源，不会提高多少系统负荷，在这一点上，rsync就远远不及它了。虽然 rsync比scp会快一点，但当小文件众多的情况下，rsync会导致硬盘I/O非常高，而scp基本不影响系统正常使用。</p>
<h2 id="1．命令格式"><a href="#1．命令格式" class="headerlink" title="1．命令格式"></a>1．命令格式</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">scp [参数] [原路径] [目标路径]</div></pre></td></tr></table></figure>
<h2 id="2．命令功能"><a href="#2．命令功能" class="headerlink" title="2．命令功能"></a>2．命令功能</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;scp是 secure copy的缩写, scp是linux系统下基于ssh登陆进行安全的远程文件拷贝命令。linux的scp命令可以在linux服务器之间复制文件和目录。</p>
<h2 id="3．命令参数"><a href="#3．命令参数" class="headerlink" title="3．命令参数"></a>3．命令参数</h2><ul>
<li>-1  强制scp命令使用协议ssh1  </li>
<li>-2  强制scp命令使用协议ssh2  </li>
<li>-4  强制scp命令只使用IPv4寻址  </li>
<li>-6  强制scp命令只使用IPv6寻址  </li>
<li>-B  使用批处理模式（传输过程中不询问传输口令或短语）  </li>
<li>-C  允许压缩。（将-C标志传递给ssh，从而打开压缩功能）  </li>
<li>-p 保留原文件的修改时间，访问时间和访问权限。  </li>
<li>-q  不显示传输进度条。  </li>
<li>-r  递归复制整个目录。  </li>
<li>-v 详细方式显示输出。scp和ssh(1)会显示出整个过程的调试信息。这些信息用于调试连接，验证和配置问题。   </li>
<li>-c cipher  以cipher将数据传输进行加密，这个选项将直接传递给ssh。   </li>
<li>-F ssh_config  指定一个替代的ssh配置文件，此参数直接传递给ssh。  </li>
<li>-i identity_file  从指定文件中读取传输时使用的密钥文件，此参数直接传递给ssh。    </li>
<li>-l limit  限定用户所能使用的带宽，以Kbit/s为单位。     </li>
<li>-o ssh_option  如果习惯于使用ssh_config(5)中的参数传递方式，   </li>
<li>-P port  注意是大写的P, port是指定数据传输用到的端口号   </li>
<li>-S program  指定加密传输时所使用的程序。此程序必须能够理解ssh(1)的选项。</li>
</ul>
<h2 id="4．使用实例"><a href="#4．使用实例" class="headerlink" title="4．使用实例"></a>4．使用实例</h2><h3 id="scp命令的实际应用概述："><a href="#scp命令的实际应用概述：" class="headerlink" title="scp命令的实际应用概述："></a>scp命令的实际应用概述：</h3><h4 id="从本地服务器复制到远程服务器："><a href="#从本地服务器复制到远程服务器：" class="headerlink" title="从本地服务器复制到远程服务器："></a>从本地服务器复制到远程服务器：</h4><h5 id="1-复制文件："><a href="#1-复制文件：" class="headerlink" title="(1) 复制文件："></a>(1) 复制文件：</h5><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;命令格式：  </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">scp local_file remote_username@remote_ip:remote_folder</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;或者  </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">scp local_file remote_username@remote_ip:remote_file</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;或者  </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">scp local_file remote_ip:remote_folder</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;或者  </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">scp local_file remote_ip:remote_file</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;第1,2个指定了用户名，命令执行后需要输入用户密码，第1个仅指定了远程的目录，文件名字不变，第2个指定了文件名</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;第3,4个没有指定用户名，命令执行后需要输入用户名和密码，第3个仅指定了远程的目录，文件名字不变，第4个指定了文件名   </p>
<h5 id="2-复制目录："><a href="#2-复制目录：" class="headerlink" title="(2) 复制目录："></a>(2) 复制目录：</h5><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;命令格式：  </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">scp -r local_folder remote_username@remote_ip:remote_folder</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;或者  </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">scp -r local_folder remote_ip:remote_folder</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;第1个指定了用户名，命令执行后需要输入用户密码；</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;第2个没有指定用户名，命令执行后需要输入用户名和密码；</p>
<h4 id="从远程服务器复制到本地服务器："><a href="#从远程服务器复制到本地服务器：" class="headerlink" title="从远程服务器复制到本地服务器："></a>从远程服务器复制到本地服务器：</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;从远程复制到本地的scp命令与上面的命令雷同，只要将从本地复制到远程的命令后面2个参数互换顺序就行了。</p>
<h3 id="实例1：从远处复制文件到本地目录"><a href="#实例1：从远处复制文件到本地目录" class="headerlink" title="实例1：从远处复制文件到本地目录"></a>实例1：从远处复制文件到本地目录</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">scp root@192.168.120.204:/opt/soft/nginx-0.5.38.tar.gz /opt/soft/</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># cd /opt/soft/</span></div><div class="line">[root@localhost soft]<span class="comment"># ll</span></div><div class="line">总计 80072</div><div class="line">drwxr-xr-x 12 root root     4096 09-21 18:40 fms3.5</div><div class="line">drwxr-xr-x  3 root root     4096 09-21 17:58 fms4.5</div><div class="line">drwxr-xr-x 10 root root     4096 10-30 17:15 jdk1.6.0_16</div><div class="line">drwxr-xr-x 10 root root     4096 09-17 19:27 jdk1.6.0_16.bak</div><div class="line">-rwxr-xr-x  1 root root 81871260 2009-12-21 jdk-6u16-linux-x64.bin</div><div class="line">drwxrwxrwx  2 root root     4096 09-21 01:16 mysql</div><div class="line">drwxr-xr-x  3 root root     4096 09-21 18:40 setup_file</div><div class="line">drwxr-xr-x  9 root root     4096 09-17 19:23 tomcat6.0.32</div><div class="line">drwxr-xr-x  9 root root     4096 2012-08-14 tomcat_7.0</div><div class="line">[root@localhost soft]<span class="comment"># scp root@192.168.120.204:/opt/soft/nginx-0.5.38.tar.gz /opt/soft/</span></div><div class="line">root@192.168.120.204<span class="string">'s password: </span></div><div class="line"><span class="string">nginx-0.5.38.tar.gz                                                                               100%  479KB 478.7KB/s   00:00    </span></div><div class="line"><span class="string">[root@localhost soft]# ll</span></div><div class="line"><span class="string">总计 80556</span></div><div class="line"><span class="string">drwxr-xr-x 12 root root     4096 09-21 18:40 fms3.5</span></div><div class="line"><span class="string">drwxr-xr-x  3 root root     4096 09-21 17:58 fms4.5</span></div><div class="line"><span class="string">drwxr-xr-x 10 root root     4096 10-30 17:15 jdk1.6.0_16</span></div><div class="line"><span class="string">drwxr-xr-x 10 root root     4096 09-17 19:27 jdk1.6.0_16.bak</span></div><div class="line"><span class="string">-rwxr-xr-x  1 root root 81871260 2009-12-21 jdk-6u16-linux-x64.bin</span></div><div class="line"><span class="string">drwxrwxrwx  2 root root     4096 09-21 01:16 mysql</span></div><div class="line"><span class="string">-rw-r--r--  1 root root   490220 03-15 09:11 nginx-0.5.38.tar.gz</span></div><div class="line"><span class="string">drwxr-xr-x  3 root root     4096 09-21 18:40 setup_file</span></div><div class="line"><span class="string">drwxr-xr-x  9 root root     4096 09-17 19:23 tomcat6.0.32</span></div><div class="line"><span class="string">drwxr-xr-x  9 root root     4096 2012-08-14 tomcat_7.0</span></div><div class="line"><span class="string">[root@localhost soft]#</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;从192.168.120.204机器上的/opt/soft/的目录中下载nginx-0.5.38.tar.gz 文件到本地/opt/soft/目录中</p>
<h3 id="实例2：从远处复制到本地"><a href="#实例2：从远处复制到本地" class="headerlink" title="实例2：从远处复制到本地"></a>实例2：从远处复制到本地</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">scp -r root@192.168.120.204:/opt/soft/mongodb /opt/soft/</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div></pre></td><td class="code"><pre><div class="line">[root@localhost soft]<span class="comment"># ll</span></div><div class="line">总计 80556</div><div class="line">drwxr-xr-x 12 root root     4096 09-21 18:40 fms3.5</div><div class="line">drwxr-xr-x  3 root root     4096 09-21 17:58 fms4.5</div><div class="line">drwxr-xr-x 10 root root     4096 10-30 17:15 jdk1.6.0_16</div><div class="line">drwxr-xr-x 10 root root     4096 09-17 19:27 jdk1.6.0_16.bak</div><div class="line">-rwxr-xr-x  1 root root 81871260 2009-12-21 jdk-6u16-linux-x64.bin</div><div class="line">drwxrwxrwx  2 root root     4096 09-21 01:16 mysql</div><div class="line">-rw-r--r--  1 root root   490220 03-15 09:11 nginx-0.5.38.tar.gz</div><div class="line">drwxr-xr-x  3 root root     4096 09-21 18:40 setup_file</div><div class="line">drwxr-xr-x  9 root root     4096 09-17 19:23 tomcat6.0.32</div><div class="line">drwxr-xr-x  9 root root     4096 2012-08-14 tomcat_7.0</div><div class="line">[root@localhost soft]<span class="comment"># scp -r root@192.168.120.204:/opt/soft/mongodb /opt/soft/</span></div><div class="line">root@192.168.120.204<span class="string">'s password: </span></div><div class="line"><span class="string">mongodb-linux-i686-static-1.8.5.tgz                                                               100%   28MB  28.3MB/s   00:01    </span></div><div class="line"><span class="string">README                                                                                            100%  731     0.7KB/s   00:00    </span></div><div class="line"><span class="string">THIRD-PARTY-NOTICES                                                                               100% 7866     7.7KB/s   00:00    </span></div><div class="line"><span class="string">mongorestore                                                                                      100% 7753KB   7.6MB/s   00:00    </span></div><div class="line"><span class="string">mongod                                                                                            100% 7760KB   7.6MB/s   00:01    </span></div><div class="line"><span class="string">mongoexport                                                                                       100% 7744KB   7.6MB/s   00:00    </span></div><div class="line"><span class="string">bsondump                                                                                          100% 7737KB   7.6MB/s   00:00    </span></div><div class="line"><span class="string">mongofiles                                                                                        100% 7748KB   7.6MB/s   00:01    </span></div><div class="line"><span class="string">mongostat                                                                                         100% 7808KB   7.6MB/s   00:00    </span></div><div class="line"><span class="string">mongos                                                                                            100% 5262KB   5.1MB/s   00:01    </span></div><div class="line"><span class="string">mongo                                                                                             100% 3707KB   3.6MB/s   00:00    </span></div><div class="line"><span class="string">mongoimport                                                                                       100% 7754KB   7.6MB/s   00:00    </span></div><div class="line"><span class="string">mongodump                                                                                         100% 7773KB   7.6MB/s   00:00    </span></div><div class="line"><span class="string">GNU-AGPL-3.0                                                                                      100%   34KB  33.7KB/s   00:00    </span></div><div class="line"><span class="string">[root@localhost soft]# ll</span></div><div class="line"><span class="string">总计 80560</span></div><div class="line"><span class="string">drwxr-xr-x 12 root root     4096 09-21 18:40 fms3.5</span></div><div class="line"><span class="string">drwxr-xr-x  3 root root     4096 09-21 17:58 fms4.5</span></div><div class="line"><span class="string">drwxr-xr-x 10 root root     4096 10-30 17:15 jdk1.6.0_16</span></div><div class="line"><span class="string">drwxr-xr-x 10 root root     4096 09-17 19:27 jdk1.6.0_16.bak</span></div><div class="line"><span class="string">-rwxr-xr-x  1 root root 81871260 2009-12-21 jdk-6u16-linux-x64.bin</span></div><div class="line"><span class="string">drwxr-xr-x  3 root root     4096 03-15 09:18 mongodb</span></div><div class="line"><span class="string">drwxrwxrwx  2 root root     4096 09-21 01:16 mysql</span></div><div class="line"><span class="string">-rw-r--r--  1 root root   490220 03-15 09:11 nginx-0.5.38.tar.gz</span></div><div class="line"><span class="string">drwxr-xr-x  3 root root     4096 09-21 18:40 setup_file</span></div><div class="line"><span class="string">drwxr-xr-x  9 root root     4096 09-17 19:23 tomcat6.0.32</span></div><div class="line"><span class="string">drwxr-xr-x  9 root root     4096 2012-08-14 tomcat_7.0</span></div><div class="line"><span class="string">[root@localhost soft]#</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;从192.168.120.204机器上的/opt/soft/中下载mongodb 目录到本地的/opt/soft/目录来。</p>
<h3 id="实例3：上传本地文件到远程机器指定目录"><a href="#实例3：上传本地文件到远程机器指定目录" class="headerlink" title="实例3：上传本地文件到远程机器指定目录"></a>实例3：上传本地文件到远程机器指定目录</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">scp /opt/soft/nginx-0.5.38.tar.gz root@192.168.120.204:/opt/soft/scptest</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line">上传前目标机器的目标目录：</div><div class="line">[root@localhost soft]<span class="comment"># cd scptest/</span></div><div class="line">[root@localhost scptest]<span class="comment"># ll</span></div><div class="line">总计 0</div><div class="line">[root@localhost scptest]<span class="comment"># ll</span></div><div class="line"></div><div class="line">本地机器上传：</div><div class="line">[root@localhost soft]<span class="comment"># scp /opt/soft/nginx-0.5.38.tar.gz root@192.168.120.204:/opt/soft/scptest</span></div><div class="line">root@192.168.120.204<span class="string">'s password: </span></div><div class="line"><span class="string">nginx-0.5.38.tar.gz                                                                               100%  479KB 478.7KB/s   00:00    </span></div><div class="line"><span class="string">[root@localhost soft]# </span></div><div class="line"><span class="string"></span></div><div class="line"><span class="string">上传后目标机器的目标目录：</span></div><div class="line"><span class="string">[root@localhost scptest]# ll</span></div><div class="line"><span class="string">总计 484</span></div><div class="line"><span class="string">-rw-r--r-- 1 root root 490220 03-15 09:25 nginx-0.5.38.tar.gz</span></div><div class="line"><span class="string">[root@localhost scptest]#</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;复制本地opt/soft/目录下的文件nginx-0.5.38.tar.gz 到远程机器192.168.120.204的opt/soft/scptest目录</p>
<h3 id="实例4：上传本地目录到远程机器指定目录"><a href="#实例4：上传本地目录到远程机器指定目录" class="headerlink" title="实例4：上传本地目录到远程机器指定目录"></a>实例4：上传本地目录到远程机器指定目录</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">scp -r /opt/soft/mongodb root@192.168.120.204:/opt/soft/scptest</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div></pre></td><td class="code"><pre><div class="line">上传前目标机器的目标目录：</div><div class="line">[root@localhost ~]<span class="comment"># cd /opt/soft/scptest/</span></div><div class="line">[root@localhost scptest]<span class="comment"># ll</span></div><div class="line">总计 484</div><div class="line">-rw-r--r-- 1 root root 490220 03-15 09:25 nginx-0.5.38.tar.gz</div><div class="line">[root@localhost scptest]<span class="comment"># </span></div><div class="line">本地机器上传：</div><div class="line">[root@localhost ~]<span class="comment"># scp -r /opt/soft/mongodb root@192.168.120.204:/opt/soft/scptest</span></div><div class="line">root@192.168.120.204<span class="string">'s password: </span></div><div class="line"><span class="string">mongodb-linux-i686-static-1.8.5.tgz                                                               100%   28MB  28.3MB/s   00:01    </span></div><div class="line"><span class="string">README                                                                                            100%  731     0.7KB/s   00:00    </span></div><div class="line"><span class="string">THIRD-PARTY-NOTICES                                                                               100% 7866     7.7KB/s   00:00    </span></div><div class="line"><span class="string">mongorestore                                                                                      100% 7753KB   7.6MB/s   00:00    </span></div><div class="line"><span class="string">mongod                                                                                            100% 7760KB   7.6MB/s   00:01    </span></div><div class="line"><span class="string">mongoexport                                                                                       100% 7744KB   7.6MB/s   00:00    </span></div><div class="line"><span class="string">bsondump                                                                                          100% 7737KB   7.6MB/s   00:00    </span></div><div class="line"><span class="string">mongofiles                                                                                        100% 7748KB   7.6MB/s   00:00    </span></div><div class="line"><span class="string">mongostat                                                                                         100% 7808KB   7.6MB/s   00:01    </span></div><div class="line"><span class="string">mongos                                                                                            100% 5262KB   5.1MB/s   00:00    </span></div><div class="line"><span class="string">mongo                                                                                             100% 3707KB   3.6MB/s   00:00    </span></div><div class="line"><span class="string">mongoimport                                                                                       100% 7754KB   7.6MB/s   00:01    </span></div><div class="line"><span class="string">mongodump                                                                                         100% 7773KB   7.6MB/s   00:00    </span></div><div class="line"><span class="string">GNU-AGPL-3.0                                                                                      100%   34KB  33.7KB/s   00:00    </span></div><div class="line"><span class="string">[root@localhost ~]# </span></div><div class="line"><span class="string"></span></div><div class="line"><span class="string">上传后目标机器的目标目录：</span></div><div class="line"><span class="string">[root@localhost scptest]# ll</span></div><div class="line"><span class="string">总计 488</span></div><div class="line"><span class="string">drwxr-xr-x 3 root root   4096 03-15 09:33 mongodb</span></div><div class="line"><span class="string">-rw-r--r-- 1 root root 490220 03-15 09:25 nginx-0.5.38.tar.gz</span></div><div class="line"><span class="string">[root@localhost scptest]#</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;上传本地目录 /opt/soft/mongodb到远程机器192.168.120.204上/opt/soft/scptest的目录中去</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/2. Linux 命令/61. Linux 命令- rcp/">Linux 命令- rcp</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Linux命令/">Linux命令</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;rcp代表“remote file copy”（远程文件拷贝）。该命令用于在计算机之间拷贝文件。rcp命令有两种格式。第一种格式用于文件到文件的拷贝；第二种格式用于把文件或目录拷贝到另一个目录中。</p>
<h2 id="1．命令格式"><a href="#1．命令格式" class="headerlink" title="1．命令格式"></a>1．命令格式</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">rcp [参数] [源文件] [目标文件]</div></pre></td></tr></table></figure>
<h2 id="2．命令功能"><a href="#2．命令功能" class="headerlink" title="2．命令功能"></a>2．命令功能</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;rcp命令用在远端复制文件或目录，如同时指定两个以上的文件或目录，且最后的目的地是一个已经存在的目录，则它会把前面指定的所有文件或目录复制到该目录中。</p>
<h2 id="3．命令参数"><a href="#3．命令参数" class="headerlink" title="3．命令参数"></a>3．命令参数</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;各选项含义：</p>
<ul>
<li>-r 递归地把源目录中的所有内容拷贝到目的目录中。要使用这个选项，目的必须是一个目录。</li>
<li>-p 试图保留源文件的修改时间和模式，忽略umask。</li>
<li>-k 请求rcp获得在指定区域内的远程主机的Kerberos 许可，而不是获得由krb_relmofhost⑶确定的远程主机区域内的远程主机的Kerberos许可。</li>
<li>-x 为传送的所有数据打开DES加密。这会影响响应时间和CPU利用率，但是可以提高安全性。如果在文件名中指定的路径不是完整的路径名，那么这个路径被解释为相对远程机上同名用户的主目录。如果没有给出远程用户名，就使用当前用户名。如果远程机上的路径包含特殊shell字符，需要用反斜线（\\）、双引号（”）或单引号（’）括起来，使所有的shell元字符都能被远程地解释。需要说明的是，rcp不提示输入口令，它通过rsh命令来执行拷贝。</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;directory 每个文件或目录参数既可以是远程文件名也可以是本地文件名。远程文件名具有如下形式：rname@rhost：path，其中rname是远程用户名，rhost是远程计算机名，path是这个文件的路径。</p>
<h2 id="4．使用实例"><a href="#4．使用实例" class="headerlink" title="4．使用实例"></a>4．使用实例</h2><h3 id="要使用-rcp，需要具备以下条件："><a href="#要使用-rcp，需要具备以下条件：" class="headerlink" title="要使用 rcp，需要具备以下条件："></a>要使用 rcp，需要具备以下条件：</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果系统中有 /etc/hosts 文件，系统管理员应确保该文件包含要与之进行通信的远程主机的项。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;/etc/hosts 文件中有一行文字，其中包含每个远程系统的以下信息：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">internet_address   official_name   <span class="built_in">alias</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;例如：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">9.186.10.***  webserver1.com.58.webserver</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;.rhosts 文件</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;.rhosts 文件位于远程系统的主目录下，其中包含本地系统的名称和本地登录名。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;例如，远程系统的 .rhosts 文件中的项可能是：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">webserver1 root</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;其中，webserver1 是本地系统的名称，root 是本地登录名。这样，webserver1 上的 root 即可在包含 .rhosts 文件的远程系统中来回复制文件。</p>
<h3 id="配置过程"><a href="#配置过程" class="headerlink" title="配置过程:"></a>配置过程:</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;只对root用户生效</p>
<ol>
<li>在双方root用户根目录下建立.rhosts文件,并将双方的hostname加进去.在此之前应在双方的 /etc/hosts文件中加入对方的IP和hostname</li>
<li>把rsh服务启动起来,redhat默认是不启动的。<br>方法：用执行ntsysv命令,在rsh选项前用空格键选中,确定退出。然后执行：<br>service xinetd restart即可。</li>
<li>到/etc/pam.d/目录下,把rsh文件中的auth required /lib/security/pam_securetty.so<br>一行用“#”注释掉即可。（只有注释掉这一行，才能用root用户登录）</li>
</ol>
<h3 id="命令使用"><a href="#命令使用" class="headerlink" title="命令使用:"></a>命令使用:</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;将文件复制到远程系统<br>要将本地系统中的文件复制到远程系统，请使用以下命令：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">rcplocal_fileremote_hostname:remote_fileEnter</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注意，如果当前目录下没有 local_file，则除本地文件名外，还需要提供相对路径（自当前目录开始）或绝对路径名（自 / 开始）。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;仅当希望将 remote_hostname 上的 remote_file 放到其他目录（远程主目录除外）下时，才需要为其指定完整的（绝对）路径。</p>
<h3 id="使用实例1-将当前目录下的-test1-复制到名为-webserver1的远程系统"><a href="#使用实例1-将当前目录下的-test1-复制到名为-webserver1的远程系统" class="headerlink" title="使用实例1:将当前目录下的 test1 复制到名为 webserver1的远程系统"></a>使用实例1:将当前目录下的 test1 复制到名为 webserver1的远程系统</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">rcp test1 webserver1:/home/root/test3</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在这种情况下，test1 被复制到远程子目录 test3下，名称仍为 test1 。如果仅提供了远程主机名，rcp 将把 test1 复制到远程主目录下，名称仍为 test1 。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;还可以在目的目录中包含文件名。例如，将文件复制到名为 webserver1的系统中：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">rcp test1 webserver1:/home/root/test3</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在这种情况下，将 test1 复制到远程目录root 下并将其命名为 test3。</p>
<h3 id="使用实例2：从远程系统复制文件：要将远程系统中的文件复制到本地目录下"><a href="#使用实例2：从远程系统复制文件：要将远程系统中的文件复制到本地目录下" class="headerlink" title="使用实例2：从远程系统复制文件：要将远程系统中的文件复制到本地目录下"></a>使用实例2：从远程系统复制文件：要将远程系统中的文件复制到本地目录下</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">rcp remote_hostname:remote_file local_fileEnter</div></pre></td></tr></table></figure>
<h3 id="使用实例-3-将远程系统-webserver1中的-test2-复制到当前目录："><a href="#使用实例-3-将远程系统-webserver1中的-test2-复制到当前目录：" class="headerlink" title="使用实例:3:将远程系统 webserver1中的 test2 复制到当前目录："></a>使用实例:3:将远程系统 webserver1中的 test2 复制到当前目录：</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">rcp webserver1:/home/root/test2 .Enter</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;点 (.) 是“当前目录”的简写形式。在这种情况下，远程目录中的 test2 被复制到当前目录下，名称仍为 test2 。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果希望用新名称复制文件，请提供目标文件名。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果希望将 test2 复制到本地系统中的其他目录下，请使用以下绝对或相对路径名：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">rcp webserver1:/home/root/test2 otherdir/ Enter</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;或者，如果希望用其他文件名将文件复制到其他目录下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">rcp webserver1:/home/root/test2 otherdir/otherfile Enter</div></pre></td></tr></table></figure>
<h3 id="使用实例4：将目录复制到远程系统：要将本地目录及其文件和子目录复制到远程系统，请同时使用-rcp-和-r（递归）选项。"><a href="#使用实例4：将目录复制到远程系统：要将本地目录及其文件和子目录复制到远程系统，请同时使用-rcp-和-r（递归）选项。" class="headerlink" title="使用实例4：将目录复制到远程系统：要将本地目录及其文件和子目录复制到远程系统，请同时使用 rcp 和 -r（递归）选项。"></a>使用实例4：将目录复制到远程系统：要将本地目录及其文件和子目录复制到远程系统，请同时使用 rcp 和 -r（递归）选项。</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">rcp –r local_dir remote_hostname:remote_dir Enter</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果当前目录下没有 local_dir，则除本地目录名外，还需要提供相对路径名（自当前目录开始）或绝对路径名（自 / 顶级目录开始）。另外，如果主目录下没有 remote_dir，则 remote_dir 将需要一个相对路径（自主目录开始）或绝对路径（自 / 开始）。</p>
<h3 id="使用实例5-要将名为-work-的子目录完整地复制到-webserver1远程计算机中的主目录下名为-products-的目录，请键入以下内容："><a href="#使用实例5-要将名为-work-的子目录完整地复制到-webserver1远程计算机中的主目录下名为-products-的目录，请键入以下内容：" class="headerlink" title="使用实例5:要将名为 work 的子目录完整地复制到 webserver1远程计算机中的主目录下名为 products 的目录，请键入以下内容："></a>使用实例5:要将名为 work 的子目录完整地复制到 webserver1远程计算机中的主目录下名为 products 的目录，请键入以下内容：</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">rcp –r work webserver1:/home/root/products Enter</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;此命令在 webserver1:/home/root/products 下创建名为 work 的目录及其全部内容（假定 /home/root/products 已存在于 webserver1中）。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;本示例假定用户处于包含 work 的本地目录下。否则，必须提供该目录的相对或绝对路径，如 /home/root/work。</p>
<h3 id="使用实例6：从远程系统复制目录："><a href="#使用实例6：从远程系统复制目录：" class="headerlink" title="使用实例6：从远程系统复制目录："></a>使用实例6：从远程系统复制目录：</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;要将远程目录及其所有文件和子目录复制到本地目录，请在以下语法中使用 rcp 和 -r（递归）选项。<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">rcp –r remote_hostname:remote_dir local_dir Enter</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;要将名为 work 的远程目录复制到当前目录，请键入以下内容：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">rcp –r webserver1:/home/root/work .Enter</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;点 (.) 表示当前目录。将在此目录下创建 work 目录。</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/2. Linux 命令/60. Linux 命令- telnet/">Linux 命令- telnet</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Linux命令/">Linux命令</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;telnet命令通常用来远程登录。telnet程序是基于TELNET协议的远程登录客户端程序。Telnet协议是TCP/IP协议族中的一员，是Internet远程登陆服务的标准协议和主要方式。它为用户提供了在本地计算机上完成远程主机工作的 能力。在终端使用者的电脑上使用telnet程序，用它连接到服务器。终端使用者可以在telnet程序中输入命令，这些命令会在服务器上运行，就像直接在服务器的控制台上输入一样。可以在本地就能控制服务器。要开始一个 telnet会话，必须输入用户名和密码来登录服务器。Telnet是常用的远程控制Web服务器的方法。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;但是，telnet因为采用明文传送报文，安全性不好，很多Linux服务器都不开放telnet服务，而改用更安全的ssh方式了。但仍然有很多别的系统可能采用了telnet方式来提供远程登录，因此弄清楚telnet客户端的使用方式仍是很有必要的。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;telnet命令还可做别的用途，比如确定远程服务的状态，比如确定远程服务器的某个端口是否能访问。</p>
<h2 id="1．命令格式"><a href="#1．命令格式" class="headerlink" title="1．命令格式"></a>1．命令格式</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">telnet [参数] [主机]</div></pre></td></tr></table></figure>
<h2 id="2．命令功能"><a href="#2．命令功能" class="headerlink" title="2．命令功能"></a>2．命令功能</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行telnet指令开启终端机阶段作业，并登入远端主机。</p>
<h2 id="3．命令参数"><a href="#3．命令参数" class="headerlink" title="3．命令参数"></a>3．命令参数</h2><ul>
<li>-8 允许使用8位字符资料，包括输入与输出。</li>
<li>-a 尝试自动登入远端系统。</li>
<li>-b&lt;主机别名&gt; 使用别名指定远端主机名称。</li>
<li>-c 不读取用户专属目录里的.telnetrc文件。</li>
<li>-d 启动排错模式。</li>
<li>-e&lt;脱离字符&gt; 设置脱离字符。</li>
<li>-E 滤除脱离字符。</li>
<li>-f 此参数的效果和指定”-F”参数相同。</li>
<li>-F 使用Kerberos V5认证时，加上此参数可把本地主机的认证数据上传到远端主机。</li>
<li>-k&lt;域名&gt; 使用Kerberos认证时，加上此参数让远端主机采用指定的领域名，而非该主机的域名。</li>
<li>-K 不自动登入远端主机。</li>
<li>-l&lt;用户名称&gt; 指定要登入远端主机的用户名称。</li>
<li>-L 允许输出8位字符资料。</li>
<li>-n&lt;记录文件&gt; 指定文件记录相关信息。</li>
<li>-r 使用类似rlogin指令的用户界面。</li>
<li>-S&lt;服务类型&gt; 设置telnet连线所需的IP TOS信息。</li>
<li>-x 假设主机有支持数据加密的功能，就使用它。</li>
<li>-X&lt;认证形态&gt; 关闭指定的认证形态。</li>
</ul>
<h2 id="4．使用实例"><a href="#4．使用实例" class="headerlink" title="4．使用实例"></a>4．使用实例</h2><h3 id="实例1：远程服务器无法访问"><a href="#实例1：远程服务器无法访问" class="headerlink" title="实例1：远程服务器无法访问"></a>实例1：远程服务器无法访问</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">telnet 192.168.120.206</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># telnet 192.168.120.209</span></div><div class="line">Trying 192.168.120.209...</div><div class="line">telnet: connect to address 192.168.120.209: No route to host</div><div class="line">telnet: Unable to connect to remote host: No route to host</div><div class="line">[root@localhost ~]<span class="comment">#</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;处理这种情况方法：</p>
<ol>
<li>确认ip地址是否正确？</li>
<li>确认ip地址对应的主机是否已经开机？</li>
<li>如果主机已经启动，确认路由设置是否设置正确？（使用route命令查看）</li>
<li>如果主机已经启动，确认主机上是否开启了telnet服务？（使用netstat命令查看，TCP的23端口是否有LISTEN状态的行）</li>
<li>如果主机已经启动telnet服务，确认防火墙是否放开了23端口的访问？（使用iptables-save查看）</li>
</ol>
<h2 id="实例2：域名无法解析"><a href="#实例2：域名无法解析" class="headerlink" title="实例2：域名无法解析"></a>实例2：域名无法解析</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">telnet www.baidu.com</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># telnet www.baidu.com</span></div><div class="line">www.baidu.com/telnet: Temporary failure <span class="keyword">in</span> name resolution</div><div class="line">[root@localhost ~]<span class="comment">#</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;处理这种情况方法：</p>
<ol>
<li>确认域名是否正确</li>
<li>确认本机的域名解析有关的设置是否正确（/etc/resolv.conf中nameserver的设置是否正确，如果没有，可以使用nameserver 8.8.8.8）</li>
<li>确认防火墙是否放开了UDP53端口的访问（DNS使用UDP协议，端口53，使用iptables-save查看）</li>
</ol>
<h3 id="实例3：拒绝访问"><a href="#实例3：拒绝访问" class="headerlink" title="实例3：拒绝访问"></a>实例3：拒绝访问</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># telnet 192.168.120.206</span></div><div class="line">Trying 192.168.120.206...</div><div class="line">telnet: connect to address 192.168.120.206: Connection refused</div><div class="line">telnet: Unable to connect to remote host: Connection refused</div><div class="line">[root@localhost ~]<span class="comment">#</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;处理这种情况：</p>
<ol>
<li>确认ip地址或者主机名是否正确？</li>
<li>确认端口是否正确，是否默认的23端口</li>
</ol>
<h3 id="实例4：启动telnet服务"><a href="#实例4：启动telnet服务" class="headerlink" title="实例4：启动telnet服务"></a>实例4：启动telnet服务</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">service xinetd restart</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># cd /etc/xinetd.d/</span></div><div class="line">[root@localhost xinetd.d]<span class="comment"># ll</span></div><div class="line">总计 124</div><div class="line">-rw-r--r-- 1 root root 1157 2011-05-31 chargen-dgram</div><div class="line">-rw-r--r-- 1 root root 1159 2011-05-31 chargen-stream</div><div class="line">-rw-r--r-- 1 root root  523 2009-09-04 cvs</div><div class="line">-rw-r--r-- 1 root root 1157 2011-05-31 daytime-dgram</div><div class="line">-rw-r--r-- 1 root root 1159 2011-05-31 daytime-stream</div><div class="line">-rw-r--r-- 1 root root 1157 2011-05-31 discard-dgram</div><div class="line">-rw-r--r-- 1 root root 1159 2011-05-31 discard-stream</div><div class="line">-rw-r--r-- 1 root root 1148 2011-05-31 <span class="built_in">echo</span>-dgram</div><div class="line">-rw-r--r-- 1 root root 1150 2011-05-31 <span class="built_in">echo</span>-stream</div><div class="line">-rw-r--r-- 1 root root  323 2004-09-09 eklogin</div><div class="line">-rw-r--r-- 1 root root  347 2005-09-06 ekrb5-telnet</div><div class="line">-rw-r--r-- 1 root root  326 2004-09-09 gssftp</div><div class="line">-rw-r--r-- 1 root root  310 2004-09-09 klogin</div><div class="line">-rw-r--r-- 1 root root  323 2004-09-09 krb5-telnet</div><div class="line">-rw-r--r-- 1 root root  308 2004-09-09 kshell</div><div class="line">-rw-r--r-- 1 root root  317 2004-09-09 rsync</div><div class="line">-rw-r--r-- 1 root root 1212 2011-05-31 tcpmux-server</div><div class="line">-rw-r--r-- 1 root root 1149 2011-05-31 time-dgram</div><div class="line">-rw-r--r-- 1 root root 1150 2011-05-31 time-stream</div><div class="line">[root@localhost xinetd.d]<span class="comment"># cat krb5-telnet </span></div><div class="line"><span class="comment"># default: off</span></div><div class="line"><span class="comment"># description: The kerberized telnet server accepts normal telnet sessions, \</span></div><div class="line"><span class="comment">#              but can also use Kerberos 5 authentication.</span></div><div class="line">service telnet</div><div class="line">&#123;</div><div class="line">        flags           = REUSE</div><div class="line">        socket_type     = stream        </div><div class="line">        <span class="built_in">wait</span>            = no</div><div class="line">        user            = root</div><div class="line">        server          = /usr/kerberos/sbin/telnetd</div><div class="line">        log_on_failure  += USERID</div><div class="line">        <span class="built_in">disable</span>         = yes</div><div class="line">&#125;</div><div class="line">[root@localhost xinetd.d]<span class="comment">#</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>配置参数，通常的配置如下：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div></pre></td><td class="code"><pre><div class="line">service telnet </div><div class="line">&#123; </div><div class="line"><span class="built_in">disable</span> = no <span class="comment">#启用 </span></div><div class="line">flags = REUSE <span class="comment">#socket可重用 </span></div><div class="line">socket_type = stream <span class="comment">#连接方式为TCP </span></div><div class="line"><span class="built_in">wait</span> = no <span class="comment">#为每个请求启动一个进程 </span></div><div class="line">user = root <span class="comment">#启动服务的用户为root </span></div><div class="line">server = /usr/sbin/in.telnetd <span class="comment">#要激活的进程 </span></div><div class="line">log_on_failure += USERID <span class="comment">#登录失败时记录登录用户名 </span></div><div class="line">&#125; </div><div class="line">如果要配置允许登录的客户端列表，加入 </div><div class="line">only_from = 192.168.0.2 <span class="comment">#只允许192.168.0.2登录 </span></div><div class="line">如果要配置禁止登录的客户端列表，加入 </div><div class="line">no_access = 192.168.0.&#123;2,3,4&#125; <span class="comment">#禁止192.168.0.2、192.168.0.3、192.168.0.4登录 </span></div><div class="line">如果要设置开放时段，加入 </div><div class="line">access_times = 9:00-12:00 13:00-17:00 <span class="comment"># 每天只有这两个时段开放服务（我们的上班时间：P） </span></div><div class="line">如果你有两个IP地址，一个是私网的IP地址如192.168.0.2，一个是公网的IP地址如218.75.74.83，如果你希望用户只能从私网来登录telnet服务，那么加入 </div><div class="line"><span class="built_in">bind</span> = 192.168.0.2 </div><div class="line">各配置项具体的含义和语法可参考xined配置文件属性说明（man xinetd.conf） </div><div class="line">配置端口，修改services文件：</div><div class="line"><span class="comment"># vi /etc/services </span></div><div class="line">找到以下两句 </div><div class="line">telnet 23/tcp </div><div class="line">telnet 23/udp </div><div class="line">如果前面有<span class="comment">#字符，就去掉它。telnet的默认端口是23，这个端口也是黑客端口扫描的主要对象，因此最好将这个端口修改掉，修改的方法很简单，就是将23这个数字修改掉，改成大一点的数字，比如61123。注意，1024以下的端口号是internet保留的端口号，因此最好不要用，还应该注意不要与其它服务的端口冲突。 </span></div><div class="line">启动服务：</div><div class="line">service xinetd restart</div></pre></td></tr></table></figure>
<h3 id="实例5：正常telnet"><a href="#实例5：正常telnet" class="headerlink" title="实例5：正常telnet"></a>实例5：正常telnet</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">telnet 192.168.120.204</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">[root@andy ~]<span class="comment"># telnet 192.168.120.204</span></div><div class="line">Trying 192.168.120.204...</div><div class="line">Connected to 192.168.120.204 (192.168.120.204).</div><div class="line">Escape character is <span class="string">'^]'</span>.</div><div class="line"></div><div class="line">    localhost (Linux release 2.6.18-274.18.1.el5 <span class="comment">#1 SMP Thu Feb 9 12:45:44 EST 2012) (1)</span></div><div class="line"></div><div class="line">login: root</div><div class="line">Password: </div><div class="line">Login incorrect</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;一般情况下不允许root从远程登录，可以先用普通账号登录，然后再用su -切到root用户。</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/2. Linux 命令/6. Linux 命令- rm/">Linux 命令- rm</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Linux命令/">Linux命令</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;linux中删除文件和目录的命令： rm命令。rm是常用的命令，该命令的功能为删除一个目录中的一个或多个文件或目录，它也可以将某个目录及其下的所有文件及子目录均删除。对于链接文件，只是删除了链接，原有文件均保持不变。<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;rm是一个危险的命令，使用的时候要特别当心，尤其对于新手，否则整个系统就会毁在这个命令（比如在/（根目录）下执行rm * -rf）。所以，我们在执行rm之前最好先确认一下在哪个目录，到底要删除什么东西，操作时保持高度清醒的头脑。</p>
<h2 id="1．命令格式："><a href="#1．命令格式：" class="headerlink" title="1．命令格式："></a>1．命令格式：</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">rm [选项] [文件]</div></pre></td></tr></table></figure>
<h2 id="2．命令功能："><a href="#2．命令功能：" class="headerlink" title="2．命令功能："></a>2．命令功能：</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;删除一个目录中的一个或多个文件或目录，如果没有使用- r选项，则rm不会删除目录。如果使用 rm 来删除文件，通常仍可以将该文件恢复原状。</p>
<h2 id="3．命令参数："><a href="#3．命令参数：" class="headerlink" title="3．命令参数："></a>3．命令参数：</h2><ul>
<li>-f, –force    忽略不存在的文件，从不给出提示。      </li>
<li>-i, –interactive 进行交互式删除      </li>
<li>-r, -R, –recursive   指示rm将参数中列出的全部目录和子目录均递归地删除。      </li>
<li>-v, –verbose    详细显示进行的步骤      <ul>
<li>–help     显示此帮助信息并退出      </li>
<li>–version  输出版本信息并退出      </li>
</ul>
</li>
</ul>
<h2 id="4．命令实例："><a href="#4．命令实例：" class="headerlink" title="4．命令实例："></a>4．命令实例：</h2><h3 id="实例1：删除文件file，系统会先询问是否删除。"><a href="#实例1：删除文件file，系统会先询问是否删除。" class="headerlink" title="实例1：删除文件file，系统会先询问是否删除。"></a>实例1：删除文件file，系统会先询问是否删除。</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">rm [文件名]</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">[root@localhost test1]<span class="comment"># ll</span></div><div class="line">总计 4</div><div class="line">-rw-r--r-- 1 root root 56 10-26 14:31 log.log</div><div class="line">root@localhost test1]<span class="comment"># rm log.log </span></div><div class="line">rm：是否删除 一般文件 “log.log”? y</div><div class="line">root@localhost test1]<span class="comment"># ll</span></div><div class="line">总计 0[root@localhost test1]<span class="comment">#</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明：</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;输入rm log.log命令后，系统会询问是否删除，输入y后就会删除文件，不想删除则数据n。</p>
<h3 id="实例2：强行删除file，系统不再提示。"><a href="#实例2：强行删除file，系统不再提示。" class="headerlink" title="实例2：强行删除file，系统不再提示。"></a>实例2：强行删除file，系统不再提示。</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">rm -f log1.log</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">[root@localhost test1]<span class="comment"># ll</span></div><div class="line">总计 4</div><div class="line">-rw-r--r-- 1 root root 23 10-26 14:40 log1.log</div><div class="line">[root@localhost test1]<span class="comment"># rm -f log1.log </span></div><div class="line">[root@localhost test1]<span class="comment"># ll</span></div><div class="line">总计 0</div><div class="line">[root@localhost test1]<span class="comment">#</span></div></pre></td></tr></table></figure>
<h3 id="实例3：删除任何-log文件；删除前逐一询问确认"><a href="#实例3：删除任何-log文件；删除前逐一询问确认" class="headerlink" title="实例3：删除任何.log文件；删除前逐一询问确认"></a>实例3：删除任何.log文件；删除前逐一询问确认</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">rm -i *.<span class="built_in">log</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">[root@localhost test1]<span class="comment"># ll</span></div><div class="line">总计 8</div><div class="line">-rw-r--r-- 1 root root 11 10-26 14:45 log1.log</div><div class="line">-rw-r--r-- 1 root root 24 10-26 14:45 log2.log</div><div class="line">[root@localhost test1]<span class="comment"># rm -i *.log</span></div><div class="line">rm：是否删除 一般文件 “log1.log”? y</div><div class="line">rm：是否删除 一般文件 “log2.log”? y</div><div class="line">[root@localhost test1]<span class="comment"># ll</span></div><div class="line">总计 0</div><div class="line">[root@localhost test1]<span class="comment">#</span></div></pre></td></tr></table></figure>
<h3 id="实例4：将-test1子目录及子目录中所有档案删除"><a href="#实例4：将-test1子目录及子目录中所有档案删除" class="headerlink" title="实例4：将 test1子目录及子目录中所有档案删除"></a>实例4：将 test1子目录及子目录中所有档案删除</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">rm -r test1</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ll</span></div><div class="line">总计 24</div><div class="line">drwxr-xr-x 7 root root 4096 10-25 18:07 scf</div><div class="line">drwxr-xr-x 2 root root 4096 10-26 14:51 test1</div><div class="line">drwxr-xr-x 3 root root 4096 10-25 17:44 test2</div><div class="line">drwxrwxrwx 2 root root 4096 10-25 17:46 test3</div><div class="line">drwxr-xr-x 2 root root 4096 10-25 17:56 test4</div><div class="line">drwxr-xr-x 3 root root 4096 10-25 17:56 test5</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># rm -r test1</span></div><div class="line">rm：是否进入目录 “test1”? y</div><div class="line">rm：是否删除 一般文件 “test1/log3.log”? y</div><div class="line">rm：是否删除 目录 “test1”? y</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ll</span></div><div class="line">总计 20drwxr-xr-x 7 root root 4096 10-25 18:07 scf</div><div class="line">drwxr-xr-x 3 root root 4096 10-25 17:44 test2</div><div class="line">drwxrwxrwx 2 root root 4096 10-25 17:46 test3</div><div class="line">drwxr-xr-x 2 root root 4096 10-25 17:56 test4</div><div class="line">drwxr-xr-x 3 root root 4096 10-25 17:56 test5</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment">#</span></div></pre></td></tr></table></figure>
<h3 id="实例5：rm-rf-test2命令会将-test2-子目录及子目录中所有档案删除-并且不用一一确认"><a href="#实例5：rm-rf-test2命令会将-test2-子目录及子目录中所有档案删除-并且不用一一确认" class="headerlink" title="实例5：rm -rf test2命令会将 test2 子目录及子目录中所有档案删除,并且不用一一确认"></a>实例5：rm -rf test2命令会将 test2 子目录及子目录中所有档案删除,并且不用一一确认</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">rm -rf  test2</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># rm -rf test2</span></div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ll</span></div><div class="line">总计 16</div><div class="line">drwxr-xr-x 7 root root 4096 10-25 18:07 scf</div><div class="line">drwxrwxrwx 2 root root 4096 10-25 17:46 test3</div><div class="line">drwxr-xr-x 2 root root 4096 10-25 17:56 test4</div><div class="line">drwxr-xr-x 3 root root 4096 10-25 17:56 test5</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment">#</span></div></pre></td></tr></table></figure>
<h3 id="实例6：删除以-f-开头的文件"><a href="#实例6：删除以-f-开头的文件" class="headerlink" title="实例6：删除以 -f 开头的文件"></a>实例6：删除以 -f 开头的文件</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">rm -- -f</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># touch -- -f</span></div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ls -- -f</span></div><div class="line">-f[root@localhost <span class="built_in">test</span>]<span class="comment"># rm -- -f</span></div><div class="line">rm：是否删除 一般空文件 “-f”? y</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ls -- -f</span></div><div class="line">ls: -f: 没有那个文件或目录</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment">#</span></div><div class="line">也可以使用下面的操作步骤:</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># touch ./-f</span></div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ls ./-f</span></div><div class="line">./-f[root@localhost <span class="built_in">test</span>]<span class="comment"># rm ./-f</span></div><div class="line">rm：是否删除 一般空文件 “./-f”? y</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment">#</span></div></pre></td></tr></table></figure>
<h3 id="实例7：自定义回收站功能"><a href="#实例7：自定义回收站功能" class="headerlink" title="实例7：自定义回收站功能"></a>实例7：自定义回收站功能</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="title">myrm</span></span>()&#123; D=/tmp/$(date +%Y%m%d%H%M%S); mkdir -p <span class="variable">$D</span>; mv <span class="string">"<span class="variable">$@</span>"</span> <span class="variable">$D</span> &amp;&amp; <span class="built_in">echo</span> <span class="string">"moved to <span class="variable">$D</span> ok"</span>; &#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># myrm()&#123; D=/tmp/$(date +%Y%m%d%H%M%S); mkdir -p $D; 	mv "$@" $D &amp;&amp; echo "moved to $D ok"; &#125;</span></div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># alias rm='myrm'</span></div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># touch 1.log 2.log 3.log</span></div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ll</span></div><div class="line">总计 16</div><div class="line">-rw-r--r-- 1 root root    0 10-26 15:08 1.log</div><div class="line">-rw-r--r-- 1 root root    0 10-26 15:08 2.log</div><div class="line">-rw-r--r-- 1 root root    0 10-26 15:08 3.log</div><div class="line">drwxr-xr-x 7 root root 4096 10-25 18:07 scf</div><div class="line">drwxrwxrwx 2 root root 4096 10-25 17:46 test3</div><div class="line">drwxr-xr-x 2 root root 4096 10-25 17:56 test4</div><div class="line">drwxr-xr-x 3 root root 4096 10-25 17:56 test5</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># rm [123].log</span></div><div class="line">moved to /tmp/20121026150901 ok</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ll</span></div><div class="line">总计 16drwxr-xr-x 7 root root 4096 10-25 18:07 scf</div><div class="line">drwxrwxrwx 2 root root 4096 10-25 17:46 test3</div><div class="line">drwxr-xr-x 2 root root 4096 10-25 17:56 test4</div><div class="line">drwxr-xr-x 3 root root 4096 10-25 17:56 test5</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ls /tmp/20121026150901/</span></div><div class="line">1.log  2.log  3.log</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment">#</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明：</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;上面的操作过程模拟了回收站的效果，即删除文件的时候只是把文件放到一个临时目录中，这样在需要的时候还可以恢复过来。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;参考资料：<a href="http://codingstandards.iteye.com/blog/983531" target="_blank" rel="external">我使用过的Linux命令之rm - 删除文件或目录</a> </p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/2. Linux 命令/59. Linux 命令- ss/">Linux 命令- ss</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Linux命令/">Linux命令</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;ss是Socket Statistics的缩写。顾名思义，ss命令可以用来获取socket统计信息，它可以显示和netstat类似的内容。但ss的优势在于它能够显示更多更详细的有关TCP和连接状态的信息，而且比netstat更快速更高效。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当服务器的socket连接数量变得非常大时，无论是使用netstat命令还是直接cat /proc/net/tcp，执行速度都会很慢。可能你不会有切身的感受，但请相信我，当服务器维持的连接达到上万个的时候，使用netstat等于浪费 生命，而用ss才是节省时间。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;天下武功唯快不破。ss快的秘诀在于，它利用到了TCP协议栈中tcp_diag。tcp_diag是一个用于分析统计的模块，可以获得Linux 内核中第一手的信息，这就确保了ss的快捷高效。当然，如果你的系统中没有tcp_diag，ss也可以正常运行，只是效率会变得稍慢。（但仍然比 netstat要快。）</p>
<h2 id="1-命令格式"><a href="#1-命令格式" class="headerlink" title="1.命令格式"></a>1.命令格式</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">ss [参数]</div><div class="line">ss [参数] [过滤]</div></pre></td></tr></table></figure>
<h2 id="2-命令功能"><a href="#2-命令功能" class="headerlink" title="2.命令功能"></a>2.命令功能</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;ss(Socket Statistics的缩写)命令可以用来获取 socket统计信息，此命令输出的结果类似于 netstat输出的内容，但它能显示更多更详细的 TCP连接状态的信息，且比 netstat 更快速高效。它使用了 TCP协议栈中 tcp_diag（是一个用于分析统计的模块），能直接从获得第一手内核信息，这就使得 ss命令快捷高效。在没有 tcp_diag，ss也可以正常运行。</p>
<h2 id="3-命令参数"><a href="#3-命令参数" class="headerlink" title="3.命令参数"></a>3.命令参数</h2><ul>
<li>-h, –help    帮助信息</li>
<li>-V, –version    程序版本信息</li>
<li>-n, –numeric    不解析服务名称</li>
<li>-r, –resolve        解析主机名</li>
<li>-a, –all    显示所有套接字（sockets）</li>
<li>-l, –listening    显示监听状态的套接字（sockets）</li>
<li>-o, –options        显示计时器信息</li>
<li>-e, –extended       显示详细的套接字（sockets）信息</li>
<li>-m, –memory         显示套接字（socket）的内存使用情况</li>
<li>-p, –processes    显示使用套接字（socket）的进程</li>
<li>-i, –info    显示 TCP内部信息</li>
<li>-s, –summary    显示套接字（socket）使用概况</li>
<li>-4, –ipv4           仅显示IPv4的套接字（sockets）</li>
<li>-6, –ipv6           仅显示IPv6的套接字（sockets）</li>
<li>-0, –packet            显示 PACKET 套接字（socket）</li>
<li>-t, –tcp    仅显示 TCP套接字（sockets）</li>
<li>-u, –udp    仅显示 UCP套接字（sockets）</li>
<li>-d, –dccp    仅显示 DCCP套接字（sockets）</li>
<li>-w, –raw    仅显示 RAW套接字（sockets）</li>
<li>-x, –unix    仅显示 Unix套接字（sockets）</li>
<li>-f, –family=FAMILY  显示 FAMILY类型的套接字（sockets），FAMILY可选，支持  unix, inet, inet6, link, netlink</li>
<li>-A, –query=QUERY, –socket=QUERY<ul>
<li>QUERY := {all|inet|tcp|udp|raw|unix|packet|netlink}[,QUERY]</li>
</ul>
</li>
<li>-D, –diag=FILE     将原始TCP套接字（sockets）信息转储到文件</li>
<li>-F, –filter=FILE   从文件中都去过滤器信息<ul>
<li>FILTER := [ state TCP-STATE ] [ EXPRESSION ]</li>
</ul>
</li>
</ul>
<h2 id="4-使用实例"><a href="#4-使用实例" class="headerlink" title="4.使用实例"></a>4.使用实例</h2><h3 id="实例1：显示TCP连接"><a href="#实例1：显示TCP连接" class="headerlink" title="实例1：显示TCP连接"></a>实例1：显示TCP连接</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">ss -t -a</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># ss -t -a</span></div><div class="line">State      Recv-Q Send-Q                                Local Address:Port                                    Peer Address:Port   </div><div class="line">LISTEN     0      0                                         127.0.0.1:smux                                               *:*       </div><div class="line">LISTEN     0      0                                                 *:3690                                               *:*       </div><div class="line">LISTEN     0      0                                                 *:ssh                                                *:*       </div><div class="line">ESTAB      0      0                                   192.168.120.204:ssh                                        10.2.0.68:49368   </div><div class="line">[root@localhost ~]<span class="comment">#</span></div></pre></td></tr></table></figure>
<h3 id="实例2：显示-Sockets-摘要"><a href="#实例2：显示-Sockets-摘要" class="headerlink" title="实例2：显示 Sockets 摘要"></a>实例2：显示 Sockets 摘要</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">ss -s</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># ss -s</span></div><div class="line">Total: 34 (kernel 48)</div><div class="line">TCP:   4 (estab 1, closed 0, orphaned 0, synrecv 0, timewait 0/0), ports 3</div><div class="line"></div><div class="line">Transport Total     IP        IPv6</div><div class="line">*         48        -         -        </div><div class="line">RAW       0         0         0        </div><div class="line">UDP       5         5         0        </div><div class="line">TCP       4         4         0        </div><div class="line">INET      9         9         0        </div><div class="line">FRAG      0         0         0        </div><div class="line"></div><div class="line">[root@localhost ~]<span class="comment">#</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;列出当前的established, closed, orphaned and waiting TCP sockets</p>
<h3 id="实例3：列出所有打开的网络连接端口"><a href="#实例3：列出所有打开的网络连接端口" class="headerlink" title="实例3：列出所有打开的网络连接端口"></a>实例3：列出所有打开的网络连接端口</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">ss -l</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># ss -l</span></div><div class="line">Recv-Q Send-Q                                     Local Address:Port                                         Peer Address:Port   </div><div class="line">0      0                                              127.0.0.1:smux                                                    *:*       </div><div class="line">0      0                                                      *:3690                                                    *:*       </div><div class="line">0      0                                                      *:ssh                                                     *:*       </div><div class="line">[root@localhost ~]<span class="comment">#</span></div></pre></td></tr></table></figure>
<h3 id="实例4：查看进程使用的socket"><a href="#实例4：查看进程使用的socket" class="headerlink" title="实例4：查看进程使用的socket"></a>实例4：查看进程使用的socket</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">ss -pl</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># ss -pl</span></div><div class="line">Recv-Q Send-Q                                     Local Address:Port                                         Peer Address:Port   </div><div class="line">0      0                                              127.0.0.1:smux                                                    *:*        users:((<span class="string">"snmpd"</span>,2716,8))</div><div class="line">0      0                                                      *:3690                                                    *:*        users:((<span class="string">"svnserve"</span>,3590,3))</div><div class="line">0      0                                                      *:ssh                                                     *:*        users:((<span class="string">"sshd"</span>,2735,3))</div><div class="line">[root@localhost ~]<span class="comment">#</span></div></pre></td></tr></table></figure>
<h3 id="实例5：找出打开套接字-端口应用程序"><a href="#实例5：找出打开套接字-端口应用程序" class="headerlink" title="实例5：找出打开套接字/端口应用程序"></a>实例5：找出打开套接字/端口应用程序</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">ss -lp | grep 3306</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># ss -lp|grep 1935</span></div><div class="line">0      0                            *:1935                          *:*        users:((<span class="string">"fmsedge"</span>,2913,18))</div><div class="line">0      0                    127.0.0.1:19350                         *:*        users:((<span class="string">"fmsedge"</span>,2913,17))</div><div class="line">[root@localhost ~]<span class="comment"># ss -lp|grep 3306</span></div><div class="line">0      0                            *:3306                          *:*        users:((<span class="string">"mysqld"</span>,2871,10))</div><div class="line">[root@localhost ~]<span class="comment">#</span></div></pre></td></tr></table></figure>
<h3 id="实例6：显示所有UDP-Sockets"><a href="#实例6：显示所有UDP-Sockets" class="headerlink" title="实例6：显示所有UDP Sockets"></a>实例6：显示所有UDP Sockets</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">ss -u -a</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># ss -u -a</span></div><div class="line">State      Recv-Q Send-Q                                Local Address:Port                                    Peer Address:Port   </div><div class="line">UNCONN     0      0                                         127.0.0.1:syslog                                             *:*       </div><div class="line">UNCONN     0      0                                                 *:snmp                                               *:*       </div><div class="line">ESTAB      0      0                                   192.168.120.203:39641                                  10.58.119.119:domain </div><div class="line">[root@localhost ~]<span class="comment">#</span></div></pre></td></tr></table></figure>
<h3 id="实例7：显示所有状态为established的SMTP连接"><a href="#实例7：显示所有状态为established的SMTP连接" class="headerlink" title="实例7：显示所有状态为established的SMTP连接"></a>实例7：显示所有状态为established的SMTP连接</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">ss -o state established <span class="string">'( dport = :smtp or sport = :smtp )'</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># ss -o state established '( dport = :smtp or sport = :smtp )' </span></div><div class="line">Recv-Q Send-Q                                     Local Address:Port                                         Peer Address:Port   </div><div class="line">[root@localhost ~]<span class="comment">#</span></div></pre></td></tr></table></figure>
<h3 id="实例8：显示所有状态为Established的HTTP连接"><a href="#实例8：显示所有状态为Established的HTTP连接" class="headerlink" title="实例8：显示所有状态为Established的HTTP连接"></a>实例8：显示所有状态为Established的HTTP连接</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">ss -o state established <span class="string">'( dport = :http or sport = :http )'</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># ss -o state established '( dport = :http or sport = :http )' </span></div><div class="line">Recv-Q Send-Q                                     Local Address:Port                                         Peer Address:Port   </div><div class="line">0      0                                              75.126.153.214:2164                                        192.168.10.42:http    </div><div class="line">[root@localhost ~]<span class="comment">#</span></div></pre></td></tr></table></figure>
<h3 id="实例9：列举出处于-FIN-WAIT-1状态的源端口为-80或者-443，目标网络为-193-233-7-24所有-tcp套接字"><a href="#实例9：列举出处于-FIN-WAIT-1状态的源端口为-80或者-443，目标网络为-193-233-7-24所有-tcp套接字" class="headerlink" title="实例9：列举出处于 FIN-WAIT-1状态的源端口为 80或者 443，目标网络为 193.233.7/24所有 tcp套接字"></a>实例9：列举出处于 FIN-WAIT-1状态的源端口为 80或者 443，目标网络为 193.233.7/24所有 tcp套接字</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">ss -o state fin-wait-1 <span class="string">'( sport = :http or sport = :https )'</span> dst 193.233.7/24</div></pre></td></tr></table></figure>
<h3 id="实例10：用TCP-状态过滤Sockets"><a href="#实例10：用TCP-状态过滤Sockets" class="headerlink" title="实例10：用TCP 状态过滤Sockets:"></a>实例10：用TCP 状态过滤Sockets:</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">ss -4 state FILTER-NAME-HERE </div><div class="line">ss -6 state FILTER-NAME-HERE</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment">#ss -4 state closing </span></div><div class="line">Recv-Q Send-Q                                                  Local Address:Port                                                      Peer Address:Port </div><div class="line">1      11094                                                  75.126.153.214:http                                                      192.168.10.42:4669</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;FILTER-NAME-HERE 可以代表以下任何一个：</p>
<ul>
<li>established      </li>
<li>syn-sent      </li>
<li>syn-recv      </li>
<li>fin-wait-1      </li>
<li>fin-wait-2      </li>
<li>time-wait      </li>
<li>closed      </li>
<li>close-wait      </li>
<li>last-ack      </li>
<li>listen      </li>
<li>closing      </li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;all : 所有以上状态</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;connected : 除了listen and closed的所有状态</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;synchronized :所有已连接的状态除了syn-sent</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;bucket : 显示状态为maintained as minisockets,如：time-wait和syn-recv.</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;big : 和bucket相反.</p>
<h3 id="实例11：匹配远程地址和端口号"><a href="#实例11：匹配远程地址和端口号" class="headerlink" title="实例11：匹配远程地址和端口号"></a>实例11：匹配远程地址和端口号</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">ss dst ADDRESS_PATTERN</div><div class="line">ss dst 192.168.1.5</div><div class="line">ss dst 192.168.119.113:http </div><div class="line">ss dst 192.168.119.113:smtp </div><div class="line">ss dst 192.168.119.113:443</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># ss dst 192.168.119.113</span></div><div class="line">State      Recv-Q Send-Q                                Local Address:Port                                    Peer Address:Port   </div><div class="line">ESTAB      0      0                                   192.168.119.103:16014                                192.168.119.113:20229   </div><div class="line">ESTAB      0      0                                   192.168.119.103:16014                                192.168.119.113:61056   </div><div class="line">ESTAB      0      0                                   192.168.119.103:16014                                192.168.119.113:61623   </div><div class="line">ESTAB      0      0                                   192.168.119.103:16014                                192.168.119.113:60924   </div><div class="line">ESTAB      0      0                                   192.168.119.103:16050                                192.168.119.113:43701   </div><div class="line">ESTAB      0      0                                   192.168.119.103:16073                                192.168.119.113:32930   </div><div class="line">ESTAB      0      0                                   192.168.119.103:16073                                192.168.119.113:49318   </div><div class="line">ESTAB      0      0                                   192.168.119.103:16014                                192.168.119.113:3844    </div><div class="line">[root@localhost ~]<span class="comment"># ss dst 192.168.119.113:http</span></div><div class="line">State      Recv-Q Send-Q                                Local Address:Port                                    Peer Address:Port   </div><div class="line">[root@localhost ~]<span class="comment"># ss dst 192.168.119.113:3844</span></div><div class="line">State      Recv-Q Send-Q                                Local Address:Port                                    Peer Address:Port   </div><div class="line">ESTAB      0      0                                   192.168.119.103:16014                                192.168.119.113:3844    </div><div class="line">[root@localhost ~]<span class="comment">#</span></div></pre></td></tr></table></figure>
<h3 id="实例12：匹配本地地址和端口号"><a href="#实例12：匹配本地地址和端口号" class="headerlink" title="实例12：匹配本地地址和端口号"></a>实例12：匹配本地地址和端口号</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">ss src ADDRESS_PATTERN</div><div class="line">ss src 192.168.119.103</div><div class="line">ss src 192.168.119.103:http</div><div class="line">ss src 192.168.119.103:80</div><div class="line">ss src 192.168.119.103:smtp</div><div class="line">ss src 192.168.119.103:25</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># ss src 192.168.119.103:16021</span></div><div class="line">State      Recv-Q Send-Q                                Local Address:Port                                    Peer Address:Port   </div><div class="line">ESTAB      0      0                                   192.168.119.103:16021                                192.168.119.201:63054   </div><div class="line">ESTAB      0      0                                   192.168.119.103:16021                                192.168.119.201:62894   </div><div class="line">ESTAB      0      0                                   192.168.119.103:16021                                192.168.119.201:63055   </div><div class="line">ESTAB      0      0                                   192.168.119.103:16021                                192.168.119.201:2274    </div><div class="line">ESTAB      0      0                                   192.168.119.103:16021                                192.168.119.201:44784   </div><div class="line">ESTAB      0      0                                   192.168.119.103:16021                                192.168.119.201:7233    </div><div class="line">ESTAB      0      0                                   192.168.119.103:16021                                192.168.119.103:58660   </div><div class="line">ESTAB      0      0                                   192.168.119.103:16021                                192.168.119.201:44822   </div><div class="line">ESTAB      0      0                                   192.168.119.103:16021                                     10.2.1.206:56737   </div><div class="line">ESTAB      0      0                                   192.168.119.103:16021                                     10.2.1.206:57487   </div><div class="line">ESTAB      0      0                                   192.168.119.103:16021                                     10.2.1.206:56736   </div><div class="line">ESTAB      0      0                                   192.168.119.103:16021                                     10.2.1.206:64652   </div><div class="line">ESTAB      0      0                                   192.168.119.103:16021                                     10.2.1.206:56586   </div><div class="line">ESTAB      0      0                                   192.168.119.103:16021                                     10.2.1.206:64653   </div><div class="line">ESTAB      0      0                                   192.168.119.103:16021                                     10.2.1.206:56587   </div><div class="line">[root@localhost ~]<span class="comment">#</span></div></pre></td></tr></table></figure>
<h3 id="实例13：将本地或者远程端口和一个数比较"><a href="#实例13：将本地或者远程端口和一个数比较" class="headerlink" title="实例13：将本地或者远程端口和一个数比较"></a>实例13：将本地或者远程端口和一个数比较</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">ss dport OP PORT </div><div class="line">ss sport OP PORT</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># ss  sport = :http </span></div><div class="line">[root@localhost ~]<span class="comment"># ss  dport = :http </span></div><div class="line">[root@localhost ~]<span class="comment"># ss  dport \&gt; :1024 </span></div><div class="line">[root@localhost ~]<span class="comment"># ss  sport \&gt; :1024 </span></div><div class="line">[root@localhost ~]<span class="comment"># ss sport \&lt; :32000 </span></div><div class="line">[root@localhost ~]<span class="comment"># ss  sport eq :22 </span></div><div class="line">[root@localhost ~]<span class="comment"># ss  dport != :22 </span></div><div class="line">[root@localhost ~]<span class="comment"># ss  state connected sport = :http </span></div><div class="line">[root@localhost ~]<span class="comment"># ss \( sport = :http or sport = :https \) </span></div><div class="line">[root@localhost ~]<span class="comment"># ss -o state fin-wait-1 \( sport = :http or sport = :https \) dst 192.168.1/24</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;ss dport OP PORT 远程端口和一个数比较；ss sport OP PORT 本地端口和一个数比较。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>OP 可以代表以下任意一个:</strong> </p>
<ul>
<li>&lt;= or le : 小于或等于端口号</li>
<li><blockquote>
<p>= or ge : 大于或等于端口号</p>
</blockquote>
</li>
<li>== or eq : 等于端口号</li>
<li>!= or ne : 不等于端口号</li>
<li>&lt; or lt : 小于端口号</li>
<li><blockquote>
<p>or gt : 大于端口号</p>
</blockquote>
</li>
</ul>
<h3 id="实例14：ss-和-netstat-效率对比"><a href="#实例14：ss-和-netstat-效率对比" class="headerlink" title="实例14：ss 和 netstat 效率对比"></a>实例14：ss 和 netstat 效率对比</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">time netstat -at</div><div class="line">time ss</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># time ss   </span></div><div class="line">real    0m0.739s</div><div class="line">user    0m0.019s</div><div class="line">sys     0m0.013s</div><div class="line">[root@localhost ~]<span class="comment"># </span></div><div class="line">[root@localhost ~]<span class="comment"># time netstat -at</span></div><div class="line">real    2m45.907s</div><div class="line">user    0m0.063s</div><div class="line">sys     0m0.067s</div><div class="line">[root@localhost ~]<span class="comment">#</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;用time 命令分别获取通过netstat和ss命令获取程序和概要占用资源所使用的时间。在服务器连接数比较多的时候，netstat的效率完全没法和ss比。</p>

	

	

</article>





	<span class="different-posts">📖 <a href="/page/51">more posts</a> 📖</span>



	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
